我正在使用 [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];
这对于 registerClass
和 dequeue
模式似乎是不可能的。我想将它保留在初始化程序中,因为它应该只运行一次,而不是每次单元格出队时。有没有合适的方法来完成这个?
最佳答案
虽然您遵循通常的单元格重用模式(就像您对注册类和出队所做的那样),但我没有看到易于实现的方法。
如果我是你,我会创建一个额外的初始化方法(不遵循 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/