Bacterial
Bacterial

Reputation: 3

Why doesn't my UITableView use prototypes for cells?

so here is my code and it doesn't work

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    if (indexPath.row == 0)
    {
        // buttonCell
        CellIdentifier = @"buttonCell";
        buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.startAddressLabel.text = @"something";

        //cell config
        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);
        NSLog(@"Cell Identifier: %@", CellIdentifier);

        return cell;
    } else if (indexPath.row == 1)
    {
        //mutableCaptionCell date&time
        CellIdentifier = @"mutableCaptionCell";
        mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //cell config
        cell.infoLabel.text = @"Время и дата";
        cell.contentLabel.text = @"";

        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);

        return cell;

    } else if (indexPath.row == 2)
    {
        //mutableCaptionCell tax
        CellIdentifier = @"mutableCaptionCell";
        mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //cell config
        cell.infoLabel.text = @"Тариф";
        cell.contentLabel.text = @"";

        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);

      return cell;
    } else if (indexPath.row == 3)
    {
        //mutableCaptionCell car
        CellIdentifier = @"mutableCaptionCell";
        mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //cell config
        cell.infoLabel.text = @"Выбор машины на карте";
        cell.contentLabel.text = @"";

        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);

        return cell;
    } else if (indexPath.row == 4)
    {
        //wishCell wishlist
        CellIdentifier = @"wishCell";
        WishCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        //cell config
        cell.infoLabel.text = @"Пожелания";
        cell.contentLabel.text = @"";

        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);

        return cell;
    } else
    {
        //debug
        NSLog(@"indexPath.row: %d", indexPath.row);
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mutableCaptionCell" forIndexPath:indexPath];

        // Configure the cell...
        cell.textLabel.text = @"ERROR CELL";

        //debug
        //NSLog(@"indexPath.row: %d", indexPath.row);

        return cell;

        ;
    }

}

I have said subclasses of UITableViewCell for each prototype, the reuse identifiers match each prototype, but what I see on runtime looks like the UITableView doesn't use these prototypes. For example, first cell must be huge one with two buttons, but instead it is shown empty. I NSLoged everything, and log show this:

2014-02-18 10:13:51.587 app[1624:70b] indexPath.row: 0
2014-02-18 10:13:51.590 app[1624:70b] Cell Identifier: buttonCell
2014-02-18 10:13:51.594 app[1624:70b] cell: <buttonCell: 0x8b95e00; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize = W; layer = <CALayer: 0x8b96000>>
2014-02-18 10:13:51.600 app[1624:70b] indexPath.row: 1
2014-02-18 10:13:51.603 app[1624:70b] indexPath.row: 2
2014-02-18 10:13:51.606 app[1624:70b] indexPath.row: 3
2014-02-18 10:13:51.610 app[1624:70b] indexPath.row: 4

but still TableView doesn't use correct prototypes.

I'm trying to use dynamic prototypes as I need to change some cells height at runtime, may be there is a way to do so with static cells ?

Sidenote: Im using Xcode 5 for iOS 7

Upvotes: 0

Views: 521

Answers (2)

user1586758
user1586758

Reputation:

It's good to use Static Cells if the number of cells are limited. When ever we scroll the tableview the cells will get laid again and again. the time taken will be very less and whatever the cell it picks up, it shows the same height to all the cells...I suggest to go for static cells or ever different tableviews if possible....

Upvotes: 0

NeverHopeless
NeverHopeless

Reputation: 11233

My guess is that, you are not initializing your cell, dequeueReusableCellWithIdentifier isn't just enough. It reuses already created cells, so you should first allocate cell if dequeue results in nil.

Possibly:

if (indexPath.row == 0)
{
    // buttonCell
    CellIdentifier = @"buttonCell";
    buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


   if(!cell)
   {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
   }

    cell.startAddressLabel.text = @"something";

    //cell config
    //debug
    NSLog(@"indexPath.row: %d", indexPath.row);
    NSLog(@"Cell Identifier: %@", CellIdentifier);

    return cell;
}

Or load cell designed using IB:

if (indexPath.row == 0)
{
    // buttonCell
    CellIdentifier = @"buttonCell";
    buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


   if(!cell)
   {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:nil options:nil] objectAtIndex:0];   // Assign reuseIdentifier from IB
   }

    cell.startAddressLabel.text = @"something";

    //cell config
    //debug
    NSLog(@"indexPath.row: %d", indexPath.row);
    NSLog(@"Cell Identifier: %@", CellIdentifier);

    return cell;
}

Hope it helps!

Upvotes: 1

Related Questions