1

Hi I'm new to IOS developement and I created a UITableView which uses custom cells that are created in a nib. Below is my code from my ViewController that is loading the cells however if I scroll up and down 3 times the app crashes because I don't think I'm reusing the cells correctly. I googled around but much of the code/solutions I found seemed to be outdated. My code is below any help is greatly appreciated!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomCell";
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }
    cell.TITLE.text = [NSString stringWithFormat:@"\"%@\"", [TITLE objectAtIndex:indexPath.row]];
    cell.desc.text = [desc objectAtIndex:indexPath.row];
    cell.votes.text = [votes objectAtIndex:indexPath.row];
    return cell;
}
Cezar
  • 55,636
  • 19
  • 86
  • 87
GFlam
  • 1,109
  • 4
  • 25
  • 38

2 Answers2

3

change the row

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];  

to be

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier: CellIdentifier];  

Go to your CustomCell .xib file in IB, look for the identifier field and set it to CustomCell

Eyal
  • 10,777
  • 18
  • 78
  • 130
  • I am using the same code as suggested above, but if I do set the cell identifier in nib file then the controls on label vanishes as I scroll up and down. However, if I skip setting the identifier in nib, then the Table View scrolls very slow but the data is coming up properly. Any ideas what might be the problem? – Anshul Apr 16 '13 at 11:14
  • First, if you skip setting the identifier, the tableview will scrolls slow because you don't reuse cells, and every cell and it's subviews is created again. I can guess that your problem is that you don't configure your cell properly in cellForRowAtIndexPath method. make sure that all the code that update your cell data is outside the if(cell == nil) statement. because if you use the identifier the cell is reused and the code in the if(cell == nil) statement doesn't get called. – Eyal Apr 19 '13 at 09:47
0

You can register the nib for tableView, like :

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CustomCell";
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        [tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    }

    //...

    return cell;
}
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
Ding
  • 1
  • 2