简体   繁体   中英

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]];

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.

Are you adding the UITableViewcontroller as addSubView ? Then you should do like this :

1) addChildViewController,then
2) addSubview
The problem is delegate and datasource of the tableview getting nil since the parent controller unable to a reference to the UITableViewController.

The problem went away when either:

  1. The table view contains many cells (10+),

  2. Each cell has a height of more than 50.

Weird.

In my case the problem was the following:

  1. I had a special case where view controller's view was not pushed or presented modally but I've added it as a subview (view.addSubview) to another view.

  2. This way the actual view was retained by its superview, but ViewController object (which was DataSource for table view) was not retained and went out of memory.

  3. I had to assign view controller to a (strong) variable so it stayed in memory.

I know this question already has an answer, but just for googlers:

In my case the problem was that the views were still in the UITableViewCell but after scrolling due to a mistake in the frame property of the views, they went under the next UITableViewCell and I could not see them.

I think as the accepted answer has clearly said that :

The problem went away when either:

  • The table view contains many cells (10+),

  • Each cell has a height of more than 50.

he had similar problem to mine.

确保已实现heightForRowAtIndexPath委托方法,并确保正确设置约束。

In my case,because I have override this function:

- (void)setFrame:(CGRect)frame
{
//    frame.origin.y += 10;
//    frame.size.height -= 10;
    [super setFrame:frame];
}

You need to calculate the correct height for each cells according to its content.

This can be done in the UITableView delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       CGFloat height;
       //Calculate height for each cell
       return height;
}

This behavior in TableView had got me head banging literally. Turns out I had used

@property (weak, nonatomic) NSMutableArray *itemsArray;

instead of

@property (strong, nonatomic) NSMutableArray *itemsArray;

Hope this information helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM