-1

I'm trying to use "reloadData" method on a uitableView, it seems like it works (both dataSource method are being called) and when I debug "cellForRowAtIndexPath" the cell that it returns is the correct cell) but I can't see the cell (numberOfRowsInSection is changing and adding blank space for each new cell - So it's not seems to be a threads problem). For example, if I have in the tableView 5 "names" (my data array called "namesArray") and I add 2 there will be 5 names and 2 nil cells presented on the tableView. The problem seems to be with cellForRowAtIndexPath.

cellForRowAtIndex:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creating "cellIdentifier"(NSString) with "Cell" as the value
NSString *cellIdentifier=@"Cell";

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];

//Creating a default cell using "cellIdentifier"(NSString)
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell==nil) {
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.textLabel.text=[namesArray objectAtIndex:indexPath.row];

//Returning cell
return cell;
}

numberOfRowsInSection:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.namesArray.count;
}

-(void)addNameToNamesArray:(NSString*)name
 {
     [self.namesArray addObject:name];
     NSLog(@"namesArray lastObject: %@",[self.namesArray lastObject]);
    //NSLog print: The correct name (the one that was added)

    [self.tableView reloadData];
 }

screenshot for "5 names and 2 nil cells": screenshot Do you have any idea what can it be?

Irfan
  • 5,070
  • 1
  • 28
  • 32
Yhper
  • 183
  • 2
  • 14

5 Answers5

0

This is likely because you have updated the data in your array, but the table view hasn't accessed that data yet, leading to the nil cells that you see. Since your cells load properly with the initial 5 element in your array, it's probable that you simply haven't refreshed the table view's data. Try using [tableView reloadData] after you have added the new elements to your array.


EDIT 1 What about changing your cellForRowAtIndexPath code to this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Creating "cellIdentifier"(NSString) with "Cell" as the value
     NSString *cellIdentifier=@"Cell";

    //Creating a default cell using "cellIdentifier"(NSString)
     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

     cell.textLabel.text = [self.namesArray objectAtIndex:indexPath.row];
     NSLog(@"name: %@", cell.textLabel.text);

    //Returning cell
     return cell;
}

Let us know the output of NSLog.

Jason
  • 635
  • 1
  • 9
  • 21
  • That's what I do, updated the post with some code, please take a look – Yhper Dec 23 '15 at 13:56
  • 3
    Shouldn't the "numberOfRowsInSection return self.namesArray.count? – Jason Dec 23 '15 at 13:59
  • It is return self.namesArray.count – Yhper Dec 23 '15 at 14:08
  • No, it always was like that – Yhper Dec 23 '15 at 14:20
  • Could you verify that "addNameToNamesArray" is being run on the main thread? Where is it being called? – Jason Dec 23 '15 at 14:34
  • And did you try removing the cell dequeue code, and replacing it with your own initialised cells? – Jason Dec 23 '15 at 14:35
  • Copy and paste the code I've got above and see what happens. It looks like it could be something to do with you reusing the cells. Otherwise the only other explanation to why they are nil is that they somehow are being run on a background thread. – Jason Dec 23 '15 at 14:41
  • Also, check your UITableView in Storyboard and make sure that you've added at least one prototype cell - with the identifier as "Cell". Refer to this: http://stackoverflow.com/questions/13379547/dequeuereusablecellwithidentifier-always-returns-nil-not-using-storyboard – Jason Dec 23 '15 at 14:45
0

change to

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creating "cellIdentifier"(NSString) with "Cell" as the value
static NSString *cellIdentifier=@"Cell";

//[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];

//Creating a default cell using "cellIdentifier"(NSString)
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.textLabel.text=[namesArray objectAtIndex:indexPath.row];

//Returning cell
return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.namesArray.count;
}

set the outlet of tableview and then in addNameToNamesArray method after adding elements reload the tableview

Sailendra
  • 1,318
  • 14
  • 29
0

You call

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];

inside cellForRowAtIndexPath!

Don't you think that's a bit late? And if it worked, you would be calling it a bit often?

And if dequeue... returns nil when a class is registered, something is badly wrong.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

1.Make sure you have initialized self.namesArray in viewDidLoad

Try the following:

[tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:cellIdentifier];

like below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];

    return cell;
 }
Irfan
  • 5,070
  • 1
  • 28
  • 32
kisekied
  • 11
  • 5
0

Your code is perfectly alright. There can be some possibilities that its not working for you.

  1. You are not initialising your datasource i.e NSMutableArray in your case.
  2. It depends how you are calling addNameToNamesArray method.

If you can share your whole code for that class as how you are calling method and every delegates you are using.

Krishna Kumar
  • 1,652
  • 9
  • 17