Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

iphone - UITableView Custom Cells Disappearing content after Scrolling

I have seen this being asked here many times but none of the solutions worked for me. Here is my code snippet:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellClassName = @"DemoTableViewCell_sched";
    DemoTableViewCell_sched *cell = [tableView dequeueReusableCellWithIdentifier:CellClassName];

    if (cell == nil)
    {
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }
    cell.fooData = [self.bugs objectAtIndex:indexPath.row];    
    return cell;
}

Then I have in ViewDidLoad

    cellLoader = [UINib nibWithNibName:@"DemoTableViewCell_sched" bundle:[NSBundle mainBundle]];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In my case, what was happening was that I had not kept a strong pointer to the tableview's dataSource. So when the tableview was scrolled, the datasource had already been freed and set to nil, which led to the tableview getting 0 for numRowsForSection and nils for cellForRowAtIndexPath.

If anyone has a similar problem, you might look if your datasource and/or custom objects are retained properly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...