Send NSArray to NSTableView - objective-c

What is the easiest way to send the data stored in an NSArray to a NSTableView and display it line by line?
For example:
NSArray has data [a, b, c]
I want the NSTableView to say:
a
b
c
The NSTableView only needs 1 column.

You don't "send" things to NSTableView. NSTableView asks you for things. It does so via the NSTableViewDataSource protocol. So all you need to do is implement the two required methods from that (-numberOfRowsInTableView: and -tableView:objectValueForTableColumn:row:), and connect the tableview's data source outlet to your object.
Documentation for NSTableViewDataSource is here: https://developer.apple.com/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html

You need to explore UITableViewDelegate and UiTableViewDataSource delegate methods:
#pragma mark --- Table View Delegate Methods ----------------------------
//Handles the selection of a cell in a table view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//Defines the number of sections in a table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//Defines the header of the section in the table view
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return nil;
}
//Defines the number of rows in each section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
//Defines the content of the table view cells
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [myDataArray objectAtIndex:[indexPath row]];//<-pay attention to this line
return cell;
}

Related

How to use TableView inside viewcontroller?

In the storyboard I have added a table view to my view controller, I have ctrl dragged the TableView to to the Viewcontroller and connected "delegate" and "datasource". In the (.h) file I have added <UITableViewDataSource,UITableViewDelegate> but when I run the app I just get a SIGABRT error (?) and the app crashes. What should I do?
So far so good, you just have to implement UITableViewDataSource and UITableViewDelegate in your implementation file.
Required functions are as follows;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [regions count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.
Region *region = [regions objectAtIndex:section];
return [region.timeZoneWrappers count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
Region *region = [regions objectAtIndex:section];
return [region name];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]];
}
Region *region = [regions objectAtIndex:indexPath.section];
TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneWrapper.localeName;
return cell;
}
Here's the link for the Apple documentation

UITableView not deselecting?

I have a very simple tableview in a xib file with it's delegate and datasource hooked up to TestVC. TestVC is simple:
#import "TestVC.h"
#implementation TestVC
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *IDEN = #"IDEN";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:IDEN];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDEN];
}
cell.textLabel.text = #"test";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (void)tableView:(UITableView *)_tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"called");
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
When I click on a cell, it doesn't get deselected. I even put a log statement there to see if didDeselectRowAtIndexPath method is being called and it is.
What am i doing wrong?
You are using the wrong method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
is the one you need

How to generate a second table view when the user touches a cell in the first (dynamic) table view (in Objective-C)?

I have a table view with dynamic cells generated in the class TableViewController. I made a segue to another table view (which inherits from TableViewController2). I want to trigger the display of the new table view when the user selects a cell from the first table view. I implemented prepareForSegue in TableViewController. The segue works, I can see the first table view pop off screen and the new table view pop in, but it's empty whatever I do. How can I trigger the display of the cells in TableViewController2?
In TableViewController:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if ([(segue.identifier) isEqualToString:#"topSegue"]){
TableViewController2 *navigationController = segue.destinationViewController;
TableViewController2 *tvc = segue.destinationViewController;
}
}
In TableViewController2:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Top 50 list";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
return cell;
}
Did you define the following two methods in your second tvc?
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

How can i reset the total rows of my table on the same button click at each time?

i am using UITableView with the following delegates.
tableView.delegate = self;
tableView.dataSource=self;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;enter code here
}
it will set the number of Rows in a section at the loading time.
it will trigger the following delegate and i can do whatever i need with table cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
However i need to create/reset the table rows dynamically(on a button click) and need to trigger
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
this delegate to set some datas to table cell.
how can i call this delegate in a button click.and also i need to reset the total rows of my table on the same button click at each time.
can any one help me to do it.
[self.tableView reloadData];
And they all lived happily ever after.
You need to change the dataSource of your table view and then call:
[tableView reloadData];
so in a button action you should:
-(void)buttonClicked{
tableViewArray = ...//load array with new data;
[self.tableView reloadData];
}
Have to set:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
**return [tableViewArray count];**
}
- (void)buttonClicked
{
tableViewArray = ... //load array with new data;
[self.tableView reloadData];
}

Need help adding array to a tableview

I'm having trouble adding cells with different content to my app. I know (or think) this needs to be done using an array, but I'm new to xcode and objective-c so I'm not exactly sure how to do this. Any help is much appreciated. Thanks.
Here is a copy of my current code:
#import "RootViewController.h"
#import "CustomCell.h"
#implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageViews.image = [UIImage imageNamed:#"captainaq_4.jpg"];
cell.firstLabel.text = #"name ";
cell.secondLabel.text = #"second name";
cell.thirdLabel.text = #"third name";
cell.thirdLabel.textAlignment = UITextAlignmentCenter;
// Configure the cell.
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
}
#end
First, note that cellForRowAtIndexPath is being called multiple times-- once for each cell. I think your best option would be to declare an array, and populate it with the objects you want displayed, as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *days[] = {#"Mon", #"Tues", #"Wed", #"Thurs", #"Fri", #"Sat", #"Sun"};
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell textLabel] setText:days[indexPath.row]];
return cell;
}
Or populate the array somewhere else, if you need to manipulate the objects within it. In that case (assuming your array is populated with strings), your call to setText would look like:
[[cell textLabel] setText:[yourArray objectAtIndex:indexPath.row]];
It also appears you want to have a header with an image in it. In that case, use:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//create your UIImageView and add your image
return yourImageView;
}
ok... as far as I have seen , I dont think the following is valid.
cell.imageViews.image = [UIImage imageNamed:#"captainaq_4.jpg"];
cell.firstLabel.text = #"name ";
cell.secondLabel.text = #"second name";
cell.thirdLabel.text = #"third name";
cell.thirdLabel.textAlignment = UITextAlignmentCenter;
Correct me if I am wrong...
As far as I understood your question, what you got to do is that create labels and add them either programmatically or through Interface Builder ( whichever is easier for you) so that you can display whatever contents you want in the cell...
Here is an example of what you can do..
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // adding indicator to the rows
}
// Configure the cell...
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"John Doe";
cell.detailTextLabel.text = #"DEPT";
//cell.imageView.image = [UIImage imageNamed:#"meeting_color.png"];
break;
case 1:
cell.textLabel.text = #"Mary Smith";
cell.detailTextLabel.text = #"DEPT";
//cell.myImageView.image = [UIImage imageNamed:#"call_color.png"];
break;
case 2:
cell.textLabel.text = #"Bob Wong";
cell.detailTextLabel.text = #"DEPT";
//cell.myImageView.image = [UIImage imageNamed:#"calendar_color.png"];
break;
default:
break;
}
return cell;
}
Again that is a very simple way you can add content to a tableview...
You can create several different UITableViewCell or your own custom cell class and add it into array and display it later under:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
With this, you can have several distinct custom cell (What I mean distinct is totally different cell, such as first cell with image while the other doesn't have image).
While the other alternative is to do it inside the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
as shown by using switch case. Either ways, it's about preference.
I suggest you to learn more about the basic use of classes, such as NSArray, NSMutableArray, and writing Objective-C language first. While it doesn't help much with learning developing app, it will speed up the learning curve during the real implementation of building program in Cocoa environment.