如果不可见,则UITableViewCell为nil [英] UITableViewCell is nil if it's not visible

查看:37
本文介绍了如果不可见,则UITableViewCell为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我正在使用 ELCTextfieldCell .想法是使用用户输入的数据进行一些计算.但是有问题.我大约有14个单元格,当然,它们不可能全部都适合屏幕.因此,当我单击 OK 时,应用程序正在检查是否已填充所有字段:

In my app I'm using ELCTextfieldCell. The idea is to use the data entered by the user for some calculations. But there is the problem. I have about 14 cells and, of course, they can't all fit on a screen. So when I click OK the app is checking if all fiels are filled in:

BOOL complete = YES;
for (int i = 0; i < [cellTextArray count] - [self.numberOfBools intValue]; i++) {
    NSIndexPath *iPath = [NSIndexPath indexPathForRow:i inSection:0];
    ELCTextfieldCell *theCell = (ELCTextfieldCell *)[self.tableView cellForRowAtIndexPath:iPath];
    if (!theCell.rightTextField.text)
        complete = NO;
}

如果所有单元格都可见,则此代码可以完美地工作,但是如果某些单元格不在,则 complete 变为 NO . gdb theCell 的输出为:

This code works perfectly if all the cells are visible, but if some are out, then the complete becomes NO. The output of theCell in gdb is:

(gdb) po theCell
Can't print the description of a NIL object.

有人可以把我推向正确的方向吗?:)

Can somebody push me in a right direction please? :)

感谢所有帮助,谢谢.

编辑

self.numberOfBools 只是一个 NSNumber ,这些行中的布尔值总数.他们正在使用 UISwitches ,而不是 UITextField 作为其他单元格,因此我从检查中排除了它们.

self.numberOfBools is just an NSNumber with total of bools in these rows. They are using UISwitches, not UITextField as the other cells, so I excluded them from the check.

推荐答案

需要根据支持单元格的数据而不是单元格本身来计算完整度.让我解释一下.

Complete needs to be calculated on the data that is backing the cells rather than on the cells themselves. Let me explain.

单元格是数据的直观表示,当不在视图中时,运行时将释放它,这就是为什么它为零的原因.

The cell is a visual representation of your data and when it is not in view the run-time will release it and that is why it is nil.

但是 cellForRowAtIndexPath:是根据数据创建单元格的吗?(或者这样做是正常的),因此,当用户更新cell.rightTextField时,您应该先更新data.rightTextField,然后再完成,应该看起来类似于...,这是伪代码不可编译

However cellForRowAtIndexPath: creates the cell from data right? (or it is normal to do so) so when the user updates the cell.rightTextField you should update data.rightTextField and then complete should be looking something akin to ... and this is pseudocode not compilable

complete = YES;
for (Data* data in myDataSet) {
    if (!data.rightTextField)
        complete = NO;
}

因此,总而言之,单元格代表数据,因此不能保证它们持久存在.您自己可以确保数据持久化;因此,请测试数据的完整性,而不是单元格的完整性.

So, in summary cells represent data and they are not guaranteed to persist. You yourself can ensure the data is persisted; therefore test for completeness on the data and not on the cells.

这篇关于如果不可见,则UITableViewCell为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆