带有自定义初始化程序的UITableViewCell dequeueReusableCellWithIdentifier [英] UITableViewCell dequeueReusableCellWithIdentifier with custom initializer
问题描述
我正在使用[UITableView registerClass: forReuseIdentifier:]
和[UITableView dequeueReusableCellWithIdentifier:]
以便将UITableViewCells排队和出队.
I'm using [UITableView registerClass: forReuseIdentifier:]
and [UITableView dequeueReusableCellWithIdentifier:]
in order to queue and dequeue UITableViewCells.
例如,在viewDidLoad中:
For example, in viewDidLoad:
[self.storeTableView registerClass:[StoreLineGraphCell class] forCellReuseIdentifier:@"StoreLineGraphCellIdentifier"];
在cellForRowAtIndexPath中:
And in cellForRowAtIndexPath:
StoreLineGraphCell *cell = (StoreLineGraphCell*)[self.storeTableView dequeueReusableCellWithIdentifier:@"StoreLineGraphCellIdentifier"];
在执行此操作时,将为UITableViewCell调用initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
初始化程序.问题是我需要使用自定义初始化程序才能创建具有必要选项的单元格.例如,可以执行以下操作:
In doing this, the initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
initializer is called for the UITableViewCell. The problem is that I need to use a custom initializer in order to create the cell with necessary options. For example, the ability to do something like this:
StoreLineGraphCell *cell = [[StoreLineGraphCell alloc] initWithReuseIdentifier:@"StoreLineGraphCell" isLocked:YES isUpcoming:YES];
使用registerClass
& dequeue
模式.我想将其保留在初始化程序中,因为它只能运行一次,而不是每次单元出队时都运行一次.有没有合适的方法来实现这一目标?
This doesn't seem possible with the registerClass
& dequeue
pattern. I'd like to keep it in an initializer as it should only be run once, not every time the cell is dequeued. Is there a proper way to accomplish this?
推荐答案
虽然您遵循通常的单元重用模式(就像您对寄存器类和出队操作一样),但我认为不容易实现的方式.
While you follow the usual pattern for cell re-usage (as you do with register class & dequeue), I do not see an easy to implement way of doing that.
如果我是我,我将创建一个附加的初始化方法(不遵循obj-c的常规init
模式)或简单地使用setter并在dequeueReusableCellWithIdentifier
调用之后进行调用.
If I were you I would create an additional initialization method (not following the usual init
pattern of obj-c) or simply setters and call that following the dequeueReusableCellWithIdentifier
call.
StoreLineGraphCell *cell = (StoreLineGraphCell*)[self.storeTableView dequeueReusableCellWithIdentifier:@"StoreLineGraphCellIdentifier"];
[cell furtherInitWithLocked:YES andUpcoming:NO]; // ... or so
这篇关于带有自定义初始化程序的UITableViewCell dequeueReusableCellWithIdentifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!