UITableViewCell dequeueReusableCellWithIdentifier with custom initializer

落花浮王杯 提交于 2020-01-12 18:44:53

问题


I'm using [UITableView registerClass: forReuseIdentifier:] and [UITableView dequeueReusableCellWithIdentifier:] in order to queue and dequeue UITableViewCells.

For example, in viewDidLoad:

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

And in cellForRowAtIndexPath:

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

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];

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?


回答1:


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.

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



回答2:


You are using the correct registerClass and dequeue methods but for invoking/setting your custom properties you should configure creating a separate method and invoke it.

Instead of this:

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

You can do this:

StoreLineGraphCell *cell = // get the dequeue cell 
[cell configure]; 

Inside the configure method you can set the properties as shown below:

-(void) configure 
{
   self.isLocked = YES; 
   self.isUpcoming = YES; 
}



回答3:


It is a common problem which I encounter today.
However, it can be solved like this.
Because the cell class registered before dequeueReusableCellWithIdentifier method called, after executed
cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier],
the cell always be not nil and return by
- initWithStyle:style reuseIdentifier:reuseIdentifier.
It seems like that you can not customize your own init method.
But, if you do not register cell class before, then dequeueReusableCellWithIdentifier will return nil if the tableview has no reusable cells.
So, we should check cell value returned by dequeueReusableCellWithIdentifier, if it is nil, then we can init it with our custom method.

cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]
if (!cell) {
    cell = /* custom init method */
}

That's done! If we want to customize subclass cell init method, so do not register cell class before.



来源:https://stackoverflow.com/questions/22612737/uitableviewcell-dequeuereusablecellwithidentifier-with-custom-initializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!