UITableView自定义单元格滚动后消失的内容

use*_*155 6 iphone xcode uitableview

我已经看过很多次这里被问过,但没有一个解决方案适合我.这是我的代码片段:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

然后我进来了 ViewDidLoad

    cellLoader = [UINib nibWithNibName:@"DemoTableViewCell_sched" bundle:[NSBundle mainBundle]];
Run Code Online (Sandbox Code Playgroud)

小智 20

就我而言,发生的事情是我没有保持对tableview的dataSource的强大指针.因此,当滚动tableview时,数据源已被释放并设置为nil,这导致tableview为numRowsForSection获取0,为cellForRowAtIndexPath获取nils.

如果有人遇到类似问题,您可能会查看是否正确保留了数据源和/或自定义对象.


小智 10

您是否将UITableViewcontroller添加为addSubView?那你应该这样做:

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.
Run Code Online (Sandbox Code Playgroud)


Mat*_*mar 6

就我而言,问题如下:

  1. 我有一个特殊情况,即视图控制器的视图没有以模态方式推送或呈现,但我已将其作为子视图 (view.addSubview) 添加到另一个视图。

  2. 这样,实际视图被其超级视图保留,但 ViewController 对象(它是表视图的数据源)没有保留并且内存不足。

  3. 我不得不将视图控制器分配给一个(强)变量,以便它留在内存中。


use*_*155 3

当出现以下任一情况时,问题就消失了:

  1. 表视图包含许多单元格(10+),

  2. 每个单元格的高度都超过50。

诡异的。