我正在尝试了解 [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
的工作原理。这是我的情况:
我已经在 IB 中设计了我的 UITableViewCell 子类,现在我正在尝试为我的子类中的单元格的子元素设置样式。不幸的是,方法 [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
似乎没有调用我的单元格子类中的任何方法。对于方法,我指的是 initWithCoder
、initWithFrame
或 init
。
这是我的实际代码:
static NSString *BasicCellIdentifier = @"BasicCell";
GSFeedBasicTableViewCell *basicCell = [tableView dequeueReusableCellWithIdentifier:BasicCellIdentifier];
我的单元格正在显示,但我想在我的子类中进一步自定义单元格以避免在我的 TableController 中设置样式代码。非常感谢任何提示。
最佳答案
使用XIB需要先注册
[self.tableView registerNib:[UINib nibWithNibName:@"IBNameofthiscell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"youridentifier"];
然后就可以使用dequeueReusableCellWithIdentifier
- 如果使用 Storyboard,不要忘记设置这个单元格的类 到您的自定义类,并将标识符设置为您的标识符。
3.然后使用这个函数
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
关于ios - 子类化 UITableViewCell & [tableView dequeueReusableCellWithIdentifier :CellIdentifier],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095060/