我正在寻找 cellForRowAtIndexPath 的替代方法。我想做的是在我的私有(private)方法中,我正在检查现有单元格上的文本是否存在于数组中,如果存在,则选择该单元格,否则保持取消选择状态。我了解到 cellForRowAtIndexPath 仅适用于可见单元格,如果单元格不可见则返回 nil。下面是我现在的代码:
- (void)selectCells:(NSArray *)array
{
for (int row = 0; row < [self.tableView numberOfRowsInSection:1]; row ++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row 1];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if ([array containsObject:cell.textLabel.text] && !cell.isSelected)
{
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
}
我还可以使用什么来实现这一点?
最佳答案
I am checking if the text on the exiting cell is present into an array
这听起来像是一个基本问题。在 Model View Controller 范式中,view 负责显示数据并与用户交互,而 model 负责存储和操作数据。您不应该查看单元格的内容并据此做出决定;您应该改为查看模型中的数据。
UITableView
的工作方式是这样的,因为如果您遵循 MVC,则完全没有必要保留不可见的单元格。每当表格需要显示一个新单元格时,它会通过调用 -tableView:cellForRowAtIndexPath:
请求您提供该单元格。
I am looking for an alternative to get all the cells on my tableview.
细胞不会同时存在。您可以想象使用不同的索引路径调用数据源的 -tableView:cellForRowAtIndexPath:
方法,就像表一样,这将返回每个单元格。但这可能不是正确的解决方案;您需要知道表格数据的存储位置,以便首先填充表格的单元格,因此您应该直接查看数据。
关于ios - cellForRowAtIndexPath 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21442513/