UITableViewCell dequeueReusableCellWithIdentifier with a custom initializer

I use [UITableView registerClass: forReuseIdentifier:]both [UITableView dequeueReusableCellWithIdentifier:]to queue and delete from UITableViewCells.

For example, in viewDidLoad:

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

And in cellForRowAtIndexPath:

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

In this case, the initializer initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifieris called for the UITableViewCell. The problem is that I need to use a custom initializer to create a cell with the necessary parameters. For example, the ability to do something like this:

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

This is not possible using the template registerClassand dequeue. I would like to save it in the initializer, since it should be run only once, not every time the cell is deleted. Is there any way to do this?

+4
3

( register dequeue), .

, ( init obj-c) , dequeueReusableCellWithIdentifier.

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

registerClass dequeue, / .

:

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

:

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

, :

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

, .
. dequeueReusableCellWithIdentifier,  cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier],
nil - initWithStyle:style reuseIdentifier:reuseIdentifier.
, init .
, , dequeueReusableCellWithIdentifier nil, tableview .
, , dequeueReusableCellWithIdentifier, nil, init .

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

It's done! If we want to set up the cell initialization method of the subclass, therefore do not register the cell class until .

0
source

Source: https://habr.com/ru/post/1533176/


All Articles