I have an NSMutableArray in with data (hard coded).
I implemented these two TableView methods and in IB, I've set the delegate and datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease];
}
cell.textLabel.text = [myArray objectAtIndex:[indexPath row]];
NSLog(@"Cell is %@", [myArray objectAtIndex:indexPath.row]);
return cell;
}
The data won't appear in the TableView. I've ran NSLog and I can see the data is in myArray.
I added the NSLog to the code and it appears the code never executes. The question is how is my array being created?
Here's the code
- (id)init:(NSMutableArray *)theArray
{
[super init];
countryTable.delegate = self;
countryTable.dataSource = self;
UITapGestureRecognizer * tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)] autorelease];
[window addGestureRecognizer:tapRecognizer];
myArray = [[NSMutableArray alloc] init];
myArray = theArray;
return self;
}
Your basic code is correct, just need to have an array (which you haven't provided here), so this would be one example:
And you can
NSLog
this:NSLog(@"Count: %i, Array: %@",[myArray count], myArray);
Output: Which should return as the
cell.textLabel.text
per your exampleYou should also make sure that you set the
UITableViewDelegate
andUITableViewDataSource
protocols in your header.