7

我在这里多次看到这个问题,但没有一个解决方案对我有用。这是我的代码片段:

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

10 回答 10

21

就我而言,发生的事情是我没有保留指向 tableview 数据源的强指针。因此,当滚动 tableview 时,数据源已经被释放并设置为 nil,这导致 tableview 的 numRowsForSection 为 0,而 cellForRowAtIndexPath 为 nils。

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

于 2014-06-28T02:15:39.840 回答
14

您是否将 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.
于 2016-03-16T13:35:59.700 回答
6

就我而言,问题如下:

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

  2. 这样,实际视图由其父视图保留,但 ViewController 对象(它是表视图的 DataSource)没有保留并且内存不足。

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

于 2014-12-19T16:10:32.000 回答
3

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

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

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

奇怪的。

于 2013-02-10T17:10:03.823 回答
1

我知道这个问题已经有了答案,但只针对谷歌人:

在我的情况下,问题是视图仍在,UITableViewCell但由于frame视图属性错误而滚动后,它们进入下一个UITableViewCell,我看不到它们。

我认为正如公认的答案已经明确表示:

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

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

  • 每个单元格的高度超过 50。

他和我有类似的问题。

于 2015-01-26T20:21:34.750 回答
1

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

于 2018-03-28T10:04:37.540 回答
0

就我而言,因为我已经覆盖了这个函数:

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

于 2019-08-21T06:41:12.653 回答
0

我有好几次类似的问题。这是非常令人不安的,因为它可能是任何东西。从错误的布局约束到严重的问题。在第一种情况下:在UITableViewCell我有

contentView.translatesAutoresizingMaskIntoConstraints = false

删除此行后,everytnig 恢复正常。

在几秒钟的情况下:我以编程方式设置了错误的布局约束:

stackView.topAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 16)

堆栈元素应该设置为顶部,而不是底部。修好这条线后一切正常。

于 2019-10-03T12:07:14.673 回答
-1

您需要根据其内容计算每个单元格的正确高度。

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       CGFloat height;
       //Calculate height for each cell
       return height;
}
于 2013-02-11T05:23:30.883 回答
-1

TableView 中的这种行为让我大吃一惊。原来我用过

@property (weak, nonatomic) NSMutableArray *itemsArray;

代替

@property (strong, nonatomic) NSMutableArray *itemsArray;

希望这些信息有所帮助。

于 2015-04-27T19:13:51.913 回答