xcode UITableView自定义单元格滚动后消失的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14798516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView Custom Cells Disappearing content after Scrolling
提问by user1235155
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
然后我在 ViewDidLoad
cellLoader = [UINib nibWithNibName:@"DemoTableViewCell_sched" bundle:[NSBundle mainBundle]];
采纳答案by user1235155
The problem went away when either:
当以下任一情况出现时,问题就消失了:
The table view contains many cells (10+),
Each cell has a height of more than 50.
表格视图包含许多单元格(10+),
每个单元格的高度都超过 50。
Weird.
奇怪的。
回答by wcobbett
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.
就我而言,发生的情况是我没有保留指向 tableview 数据源的强指针。因此,当 tableview 滚动时,数据源已经被释放并设置为 nil,这导致 tableview 为 numRowsForSection 获取 0,为 cellForRowAtIndexPath 获取 nils。
If anyone has a similar problem, you might look if your datasource and/or custom objects are retained properly.
如果有人遇到类似问题,您可能会查看您的数据源和/或自定义对象是否被正确保留。
回答by wcobbett
Are you adding the UITableViewcontroller as addSubView ? Then you should do like this :
您是否将 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.
回答by Matej Ukmar
In my case the problem was the following:
就我而言,问题如下:
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.
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.
I had to assign view controller to a (strong) variable so it stayed in memory.
我有一个特殊情况,即视图控制器的视图没有以模态方式推送或呈现,但我已将其作为子视图 (view.addSubview) 添加到另一个视图。
这样,实际视图被其超级视图保留,但 ViewController 对象(它是表视图的数据源)没有保留并且内存不足。
我不得不将视图控制器分配给一个(强)变量,以便它留在内存中。
回答by alternatiph
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.
在我的情况下,问题是视图仍然在,UITableViewCell
但是由于frame
视图属性的错误滚动后,它们进入下一个UITableViewCell
而我看不到它们。
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.
当以下任一情况出现时,问题就消失了:
表格视图包含许多单元格(10+),
每个单元格的高度都超过 50。
he had similar problem to mine.
他有和我类似的问题。
回答by Rizwan Ahmed
Make sure you have implemented heightForRowAtIndexPath delegate method and also make sure that your constraints are properly set.
确保您已实现 heightForRowAtIndexPath 委托方法,并确保您的约束设置正确。
回答by xiadeye
In my case,because I have override this function:
就我而言,因为我已经覆盖了这个函数:
- (void)setFrame:(CGRect)frame
{
// frame.origin.y += 10;
// frame.size.height -= 10;
[super setFrame:frame];
}
回答by Kirk_hehe
I had a similar issue several times. It is very disturbing since it might be anything. From wrong layout constraints to much serious problems. In first case: in UITableViewCellI had
我有几次类似的问题。这非常令人不安,因为它可能是任何东西。从错误的布局约束到严重的问题。在第一种情况下:在UITableViewCell我有
contentView.translatesAutoresizingMaskIntoConstraints = false
After removing this line everytnig went to normal.
删除这条线后,everytnig 就正常了。
In seconds case: I had wrong layout constraint set programmatically:
在几秒钟的情况下:我以编程方式设置了错误的布局约束:
stackView.topAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 16)
The stack element supposed to be set to Top, not to Bottom. After fixing this line everything went normal.
堆栈元素应该设置为顶部,而不是底部。修复这条线后一切正常。
回答by skypirate
This behavior in TableView had got me head banging literally. Turns out I had used
TableView 中的这种行为让我很头疼。原来我用过
@property (weak, nonatomic) NSMutableArray *itemsArray;
instead of
代替
@property (strong, nonatomic) NSMutableArray *itemsArray;
Hope this information helps.
希望这些信息有帮助。
回答by Saravana Vijay
You need to calculate the correct height for each cells according to its content.
您需要根据其内容计算每个单元格的正确高度。
This can be done in the UITableView delegate method:
这可以在 UITableView 委托方法中完成:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
//Calculate height for each cell
return height;
}