iOS SDK:如何在表格视图中获取不可见的单元格?

时间:2022-03-09 08:41:27

How do I get the cell for an indexPath which is not currently visible in the table? (cell is out of range)

如何获取当前在表中不可见的indexPath的单元格? (单元格超出范围)

Code for getting my cell:

获取我的手机的代码:

NSString *name = [[(ELCTextfieldCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] rightTextField] text];

-cellForRowAtIndexPath... returns nil because the cell at the required indexPath is out of range. So how do I get the correct cell and not nil?

-cellForRowAtIndexPath ...返回nil,因为所需indexPath处的单元格超出范围。那么我如何得到正确的细胞而不是零呢?

5 个解决方案

#1


4  

This is not how it works. You need to grab and store the information as soon as it is entered or changed. It may easily get out of scope and you cannot guarantee your cell lives long enough. Well, technically you can hold onto it (and always return the very same cell for the same index path), but I'd question that design.

这不是它的工作原理。您需要在输入或更改信息后立即获取并存储信息。它可能很容易超出范围,您不能保证您的细胞寿命足够长。好吧,从技术上讲,你可以抓住它(并且总是为相同的索引路径返回相同的单元格),但我会质疑那个设计。

#2


17  

The UITableView only keeps the visible cells. If you need one that isn't visible you have to call the tableView:cellForRowAtIndexPath: of the UITableView dataSource. So, if self is a class that is the dataSource:

UITableView仅保留可见单元格。如果你需要一个不可见的,你必须调用UITableView dataSource的tableView:cellForRowAtIndexPath :.所以,如果self是一个类dataSource:

UITableViewCell * cell = [self tableView:table cellForRowAtIndexPath:indexPath];

#3


2  

UITableViewCells are made to be reused/recycled in a way that the users won't need to create more cells than the number of visible ones. You usually don't need to access a cell that is not visible. It should be enough you access your datasource and get/set the correspondent data there. Cells are for showing some state of the datasource. Not the datasource itself :)

UITableViewCells可以重复使用/回收,用户不需要创建比可见数量更多的单元格。您通常不需要访问不可见的单元格。您应该可以访问数据源并在那里获取/设置相应的数据。单元格用于显示数据源的某些状态。不是数据源本身:)

Edit:

编辑:

You said you need some information (text) from one cell above, right? If you use cellForRowAtIndexPath: method the cell will be recreated but you might not get the text that was in the textfield. The reason? because probably you didn't save it somewhere else. If you did save it, then access that directly instead of going through the cell.

你说你需要上面一个单元格中的一些信息(文本),对吧?如果使用cellForRowAtIndexPath:方法,将重新创建单元格,但您可能无法获取文本字段中的文本。原因?因为可能你没有把它保存在其他地方。如果你确实保存了它,那么直接访问它而不是通过单元格。

#4


0  

Is the information to fill your table coming from an array? Could you not pull that directly out of the array at 0 index, same as your cellForRowAtIndexPath would presumably retrieve and fill that cell when it is displayed?

填充表格的信息是否来自阵列?你能否在0索引处直接将数据拉出数组,与你的cellForRowAtIndexPath相同,可能会在显示时检索并填充该单元格?

#5


0  

In continue to @Raphael's answer, Here is the (working) swift 3 solution:

继续@ Raphael的回答,这是(工作)swift 3解决方案:

UITableViewCell cell = self.tableView(self.tableView, cellForRowAt: indexPath)

UITableViewCell cell = self.tableView(self.tableView,cellForRowAt:indexPath)

#1


4  

This is not how it works. You need to grab and store the information as soon as it is entered or changed. It may easily get out of scope and you cannot guarantee your cell lives long enough. Well, technically you can hold onto it (and always return the very same cell for the same index path), but I'd question that design.

这不是它的工作原理。您需要在输入或更改信息后立即获取并存储信息。它可能很容易超出范围,您不能保证您的细胞寿命足够长。好吧,从技术上讲,你可以抓住它(并且总是为相同的索引路径返回相同的单元格),但我会质疑那个设计。

#2


17  

The UITableView only keeps the visible cells. If you need one that isn't visible you have to call the tableView:cellForRowAtIndexPath: of the UITableView dataSource. So, if self is a class that is the dataSource:

UITableView仅保留可见单元格。如果你需要一个不可见的,你必须调用UITableView dataSource的tableView:cellForRowAtIndexPath :.所以,如果self是一个类dataSource:

UITableViewCell * cell = [self tableView:table cellForRowAtIndexPath:indexPath];

#3


2  

UITableViewCells are made to be reused/recycled in a way that the users won't need to create more cells than the number of visible ones. You usually don't need to access a cell that is not visible. It should be enough you access your datasource and get/set the correspondent data there. Cells are for showing some state of the datasource. Not the datasource itself :)

UITableViewCells可以重复使用/回收,用户不需要创建比可见数量更多的单元格。您通常不需要访问不可见的单元格。您应该可以访问数据源并在那里获取/设置相应的数据。单元格用于显示数据源的某些状态。不是数据源本身:)

Edit:

编辑:

You said you need some information (text) from one cell above, right? If you use cellForRowAtIndexPath: method the cell will be recreated but you might not get the text that was in the textfield. The reason? because probably you didn't save it somewhere else. If you did save it, then access that directly instead of going through the cell.

你说你需要上面一个单元格中的一些信息(文本),对吧?如果使用cellForRowAtIndexPath:方法,将重新创建单元格,但您可能无法获取文本字段中的文本。原因?因为可能你没有把它保存在其他地方。如果你确实保存了它,那么直接访问它而不是通过单元格。

#4


0  

Is the information to fill your table coming from an array? Could you not pull that directly out of the array at 0 index, same as your cellForRowAtIndexPath would presumably retrieve and fill that cell when it is displayed?

填充表格的信息是否来自阵列?你能否在0索引处直接将数据拉出数组,与你的cellForRowAtIndexPath相同,可能会在显示时检索并填充该单元格?

#5


0  

In continue to @Raphael's answer, Here is the (working) swift 3 solution:

继续@ Raphael的回答,这是(工作)swift 3解决方案:

UITableViewCell cell = self.tableView(self.tableView, cellForRowAt: indexPath)

UITableViewCell cell = self.tableView(self.tableView,cellForRowAt:indexPath)