ios - 带有自定义初始化程序的 UITableViewCell dequeueReusableCellWithIdentifier

标签 ios iphone objective-c uitableview

我正在使用 [UITableView registerClass: forReuseIdentifier:][UITableView dequeueReusableCellWithIdentifier:] 来对 UITableViewCell 进行排队和出队。

例如,在viewDidLoad中:

[self.storeTableView registerClass:[StoreLineGraphCell 类] forCellReuseIdentifier:@"StoreLineGraphCellIdentifier"];

在 cellForRowAtIndexPath 中:

StoreLineGraphCell *cell = (StoreLineGraphCell*)[self.storeTableView dequeueReusableCellWithIdentifier:@"StoreLineGraphCellIdentifier"];

在执行此操作时,为 UITableViewCell 调用了 initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 初始化程序。问题是我需要使用自定义初始化程序来创建具有必要选项的单元格。例如,执行以下操作的能力:

StoreLineGraphCell *cell = [[StoreLineGraphCell alloc] initWithReuseIdentifier:@"StoreLineGraphCell"isLocked:YES isUpcoming:YES];

这对于 registerClassdequeue 模式似乎是不可能的。我想将它保留在初始化程序中,因为它应该只运行一次,而不是每次单元格出队时。有没有合适的方法来完成这个?

最佳答案

虽然您遵循通常的单元格重用模式(就像您对注册类和出队所做的那样),但我没有看到易于实现的方法。

如果我是你,我会创建一个额外的初始化方法(不遵循 obj-c 的通常 init 模式)或者简单地设置方法并在 dequeueReusableCellWithIdentifier 调用之后调用它.

StoreLineGraphCell *cell = (StoreLineGraphCell*)[self.storeTableView dequeueReusableCellWithIdentifier:@"StoreLineGraphCellIdentifier"];
[cell furtherInitWithLocked:YES andUpcoming:NO]; // ... or so

关于ios - 带有自定义初始化程序的 UITableViewCell dequeueReusableCellWithIdentifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22612737/

相关文章:

ios Swift 图表问题

ios - 如何使用 fetchAssests 按添加日期的顺序获取 PHAssets

iphone - IAP 完成时隐藏/取消隐藏 IBOutlet(UIButton)

ios - 绘制带孔饼图

ios - iOS 中的 SecKeyRawVerify 收到误报

iphone - 在 WebView 中显示身份验证对话框

ios - 在我的 Controller 中未调用 supportedInterfaceOrientations

ios - 如何在 Swift 中取消初始化惰性变量?

iphone - session 管理和一次性用户登录 - iphone

IOS -TextView 委托(delegate)未在页面容器 View 中调用