Jiho Kang
Jiho Kang

Reputation: 2492

Disappearing UITableViewCell on UITableView reloadRowsAtIndexPaths:

I have a few UITableViewCells that I am loading from an XIB file. Everything is great until I call the [UITableView reloadRowsAtIndexPaths:withRowAnimation:] method when the cell disappears. When I call [UITableView reloadData] everything loads find, when I scroll the cell on and off the view it will also reappear also. Weird.

I've also noticed that when I call [UITableView reloadRowsAtIndexPaths:withRowAnimation:] the UITableView will not try to reuse a cached cell and will try to get a new one with cell = [tableViewCells objectAtIndex:cellId];

Here is my code:

- (void)viewDidLoad
{
    ...
    // the objects from the XIB files are loaded onto an NSArray instance variable at viewDidLoad
    tableViewCells = [[NSBundle mainBundle] loadNibNamed:@"ProfileTableViewCell" owner:nil options:nil];
    ...
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int cellId = /* some logic to get the correct cellID */
    NSString *CellIdentifier = [NSString stringWithFormat:@"ProfileTableViewCell_%d", cellId];
    ProfileTableViewCell *cell = (ProfileTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [tableViewCells objectAtIndex:cellId];
        // additional work to setup subviews for the cell
        [cell prepareSubviews];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
}

and just in case here is some of the stuff I'm doing in [ProfileTableViewCell prepareSubviews]

- (void)prepareSubviews
{
    ...
    [self.containerView.layer setCornerRadius:3];
    [self.containerView.layer setBorderColor:UIColorFromRGB(0xCDCDCD).CGColor];
    [self.containerView.layer setBorderWidth:2.0];
    [self.containerView.layer setMasksToBounds:YES];
    [self.containerView.layer setShouldRasterize:NO];
    [self.containerView.layer setRasterizationScale:[UIScreen mainScreen].scale];
    ...
}

Thanks in advance to the awesome person that can help me out.

Upvotes: 2

Views: 1682

Answers (2)

Krizai
Krizai

Reputation: 610

It seams that reloadRowsAtIndexPaths: doesn't work when you only need to change the height for a static cell.

Try to call just

[tableView beginUpdates];
[tableView endUpdates];

As the documentation says:

You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

I'm not sure this is your problem, but the way you get your cells is not the normal way. You should make each type of cell in its own nib (with a unique identifier), and then register the nibs with registerNib:forCellReuseIdentifier:. In cellForRowAtIndexPath, just dequeue the cell you need based on the index path, and don't put anything in an if (cell == nil) clause, because that will not be invoked when you do it this way.

Upvotes: 1

Related Questions