mercredi 14 juin 2017

How to initialise my designated class once for every iteration in cell for row

My code below:

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    WARedeemReward *reward = [self feedForIndexPath:indexPath];

    WARedeemRewardCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellRedeemRewardsType];

    if (cell == nil) {
        cell = [[WARedeemRewardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellRedeemRewardsType];
    }
    /*How to make sure rewardView model is also initialise once for every cell for row call*/
    if (rewardViewModel == nil) {
        NSString *redeemRewardViewModelType = [[AWPViewFactory sharedInstance] getValue:@"WARewardHistoryViewModel"];
        rewardViewModel = [[NSClassFromString(redeemRewardViewModelType) alloc] initWithReward:reward];
        if (rewardViewModel == nil) {
            /*try to load default class*/
            rewardViewModel = (WARewardHistoryViewModel *)[[WARewardHistoryViewModel alloc]initWithReward:reward];
        }
    }
    /*How to make sure rewardView model is also initialise once for every cell for row call*/

    [WARedeemRewardCellDataBinder setData:rewardViewModel forCell:cell];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}

I want to maintain initialisation of rewardViewModel in such a way that it is initialise only once for each cellForRowAtIndexPath call. Something like cell's dequeue may be.

If i keep instance variable rewardViewModel then it will be initialise only once, and in next iterations of cellForRowAtIndexPath , same instance will be reuse again and again.

If i remove instance variable and make local variable rewardViewModel in cellForRowAtIndexPath then it will alloc init every time, which i do not want. Some thing like below:

WARewardHistoryViewModel *rewardViewModel;
NSString *redeemRewardViewModelType = [[AWPViewFactory sharedInstance] getValue:@"WARewardHistoryViewModel"];
rewardViewModel = [[NSClassFromString(redeemRewardViewModelType) alloc] initWithReward:reward];
if (rewardViewModel == nil) {
    /*try to load default class*/
    rewardViewModel = (WARewardHistoryViewModel *)[[WARewardHistoryViewModel alloc]initWithReward:reward];
}

So, How to initialise my designated class once for every iteration in cell for row.

Please suggest.

Aucun commentaire:

Enregistrer un commentaire