UITableView的自定义单元格滚动

问题描述:

我看到这个在这里被问了很多次,但没有一个解决方案的工作对我来说后消失内容。这里是我的代码片段: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; 
} 

然后我在ViewDidLoad

cellLoader = [UINib nibWithNibName:@"DemoTableViewCell_sched" bundle:[NSBundle mainBundle]]; 
+0

你是否检查过你的单元格中是否指定了标识符? – 2013-02-10 15:07:38

+0

是的。在自定义单元格中,我确定标识符是“DemoTableViewCell_sched”。我还应该检查什么? – user1235155 2013-02-10 15:16:34

+0

[This thread](http://stackoverflow.com/a/36037259/4034301)为我工作。伟大的解决方案..! – 2016-03-16 13:37:46

问题走了既可以当:

  1. 表视图包含许多细胞(10+)

  2. 每个单元具有大于50

    的高度

奇怪。

+0

此链接适用于我。 http://stackoverflow.com/a/36037259/4034301 – 2016-03-16 13:38:10

您需要根据其内容计算出正确的高度为每个细胞。

这可以在UITableView的委托方法来完成:

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

在我的情况下,发生了什么事是,我没有保持强烈的指针的tableview的数据源。所以当tableview被滚动时,数据源已经被释放并设置为nil,这导致tableview为numRowsForSection获取0,为cellForRowAtIndexPath获取nils。

如果任何人有类似的问题,你可能会如果你的数据源和/或自定义对象适当保留的样子。

+0

感谢分享,它解决了我的问题! – N0un 2017-07-20 09:15:47

在我的情况下,问题是以下几点:

  1. 我有哪里视图控制器的观点并没有推动或模态呈现一种特殊情况,但我已经添加它作为一个子视图(view.addSubview)至另一种观点。

  2. 这样,实际视图被其超级视图所保留,但ViewController对象(它是用于表视图的DataSource)未被保留并且内存不足。

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

我知道这个问题已经有了答案,只是让Google:

在我的情况下,问题是,意见仍然在UITableViewCell但在frame财产由于滚动到错误后的意见,他们在下一个UITableViewCell下,我看不到他们。

我认为,作为公认的答案已经明确表示:

问题走了既可以当:

  • 表视图包含许多细胞(10+),

  • 每个单元的高度均大于50.

他有类似的问题,我的。

在TableView中的这种行为让我头撞字面。原来我用了

@property (weak, nonatomic) NSMutableArray *itemsArray; 

,而不是

@property (strong, nonatomic) NSMutableArray *itemsArray; 

希望这个信息帮助。

+0

没有什么帮助。如果向下滚动,单元格仍然会重新生成并显示错误的UI – 2016-01-27 02:29:07

+0

,请检查在使用cellForRowAtIndexPath返回单元格时,您正在使用的NSArray/NSDictionary传递的数据是否可以再次检查约束(关于frame&代码界限)或Autolayout设置。同样考虑向下投票@wcobbett的答案,因为我刚刚在代码中写道,以避免阅读冗长的答案并指出您在理论上或代码中面临的问题。 – skypirate 2016-01-27 02:51:19

您是否将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.