Iphone UITableView自定义单元格滚动后内容消失
我在这里见过很多次这样的问题,但是没有一个解决方案对我有效。以下是我的代码片段:Iphone UITableView自定义单元格滚动后内容消失,iphone,xcode,uitableview,Iphone,Xcode,Uitableview,我在这里见过很多次这样的问题,但是没有一个解决方案对我有效。以下是我的代码片段: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellClassName = @"DemoTableViewCell_sched"; DemoTableViewCell_sched *cell = [ta
- (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]];
当以下任一情况发生时,问题消失:
奇怪。您需要根据单元格内容计算每个单元格的正确高度 这可以通过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
如果有人有类似问题,您可以查看是否正确保留了数据源和/或自定义对象。在我的案例中,问题如下:
我知道这个问题已经有了答案,但只对谷歌来说:
在我的例子中,问题是视图仍然在
UITableViewCell
中,但是在由于视图的frame
属性中的错误而滚动后,它们进入下一个UITableViewCell
下,我看不到它们
我认为,正如公认的答案明确指出的那样:
当以下任一情况发生时,问题消失:
- 表格视图包含许多单元格(10+)
- 每个单元格的高度都超过50
他和我有类似的问题。在TableView中的这种行为让我头痛不已。原来我用过
@property (weak, nonatomic) NSMutableArray *itemsArray;
而不是
@property (strong, nonatomic) NSMutableArray *itemsArray;
希望这些信息有帮助 是否将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.
确保已实现heightForRowAtIndexPath委托方法,并确保已正确设置约束 在我的例子中,因为我已经重写了这个函数:
- (void)setFrame:(CGRect)frame
{
// frame.origin.y += 10;
// frame.size.height -= 10;
[super setFrame:frame];
}
我有过几次类似的问题。这是非常令人不安的,因为它可能是任何东西。从错误的布局约束到严重的问题。 在第一种情况下:在UITableViewCell中,我 拆下这条线后,一切都恢复正常 以秒为单位:我以编程方式设置了错误的布局约束:
stackView.topAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 16)
堆栈元素应该设置为顶部,而不是底部。修好这一行后,一切正常。您是否检查过您的单元格中是否指定了标识符?是的。在自定义单元格中,我确保标识符为“DemoTableViewCell_sched”。我还应该检查什么?为我工作。很好的解决方案。。!不是很有帮助。如果向下滚动,单元格仍会重新生成并显示错误的UI。对于初学者,请检查在cellForRowAtIndexPath中返回单元格时从NSArray/NSDictionary传递的数据,可能是再次检查约束(关于代码中的帧和边界)或自动布局设置。还考虑了向下投票@ WCOBBETT答案,正如我刚才在代码中写的那样,避免阅读冗长的答案,并在理论上或代码中指定您面临的问题。只因为我在容器视图中嵌套表视图,遇到了同样的问题。将控制器分配给一个变量解决了这个问题。@Matej Ukmar:我之前也遇到过这种情况,但是是的,我遇到了同样的问题,谢谢Tona,这是正确的答案
stackView.topAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 16)