ios - 子类化 UITableViewCell & [tableView dequeueReusableCellWithIdentifier :CellIdentifier]

标签 ios objective-c swift uitableview

我正在尝试了解 [tableView dequeueReusableCellWithIdentifier:CellIdentifier] 的工作原理。这是我的情况:

我已经在 IB 中设计了我的 UITableViewCell 子类,现在我正在尝试为我的子类中的单元格的子元素设置样式。不幸的是,方法 [tableView dequeueReusableCellWithIdentifier:CellIdentifier] 似乎没有调用我的单元格子类中的任何方法。对于方法,我指的是 initWithCoderinitWithFrameinit

这是我的实际代码:

static NSString *BasicCellIdentifier = @"BasicCell";
GSFeedBasicTableViewCell *basicCell = [tableView dequeueReusableCellWithIdentifier:BasicCellIdentifier];

我的单元格正在显示,但我想在我的子类中进一步自定义单元格以避免在我的 TableController 中设置样式代码。非常感谢任何提示。

最佳答案

  1. 使用XIB需要先注册

    [self.tableView registerNib:[UINib nibWithNibName:@"IBNameofthiscell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"youridentifier"];
    

然后就可以使用dequeueReusableCellWithIdentifier

  1. 如果使用 Storyboard,不要​​忘记设置这个单元格的类 到您的自定义类,并将标识符设置为您的标识符。

enter image description here

enter image description here

3.然后使用这个函数

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];

关于ios - 子类化 UITableViewCell & [tableView dequeueReusableCellWithIdentifier :CellIdentifier],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095060/

相关文章:

ios - 拥有未经授权的 Twitter 帐户的用户在使用我的应用程序时遇到问题

swift - 在 Swift 的 TextView 中显示 JSON 数据

ios - 谷歌地图更改分段控件点击上的mapType

ios - 在 IOS 中使用 DatePicker 而不是键盘的两个单独的文本字段

objective-c - 使用自动布局将 NSView 的大小调整为其 subview

iphone - 返回一个 stringfromdate 方法

iphone - 如何设置iPhone视频输出图像大小

ios - 为什么在使用 JSONDecoder 时会出现 ‘Cannot get keyed decoding container -- found null value instead.’ 错误

ios - 清空一个 UIPageViewController(移除里面的 ViewControllers)

android - 如何在 Unity 中制作二维动画