问题描述
我正在尝试从我的 UITableViewController 中获取一个子类化的 UITableViewCell(类称为MasterCell"),并具有不断变化的 IndexPath.假设,我将在第一行获取单元格:
I'm trying to get a subclassed UITableViewCell (class is called 'MasterCell') from my UITableViewController with an changing IndexPath. Let's say, I'll get the cell at the first row:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
MasterCell *cell = (MasterCell *)[self.tableView cellForRowAtIndexPath:indexPath];
这发生在我的 viewWillAppear 方法中.不幸的是,调试时单元格为零.
This happens in my viewWillAppear method. Unfortunately, cell is nil while debugging.
我有不同的单元格/行,我必须在 viewWillAppear 方法中更改一些值.你对我有什么提示吗?
I have different cells / rows, where I have to change some values in the viewWillAppear method. Do you have any hints for me?
推荐答案
[self.tableView cellForRowAtIndexPath:indexPath]
为当前不可见的单元格返回 nil
.
[self.tableView cellForRowAtIndexPath:indexPath]
returns nil
for cells which are currently not visible.
不应使用单元格来存储模型数据,因为向上或向下滚动时会重复使用单元格.
You should not use the cells to store your model data, because cells are reused when you scroll up or down.
这篇关于使用 cellForRowAtIndexPath 从 UITableView 获取 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!