0
行
我的代碼:如何初始化我指定類一次每次迭代細胞爲
- (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];
}
/*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;
}
我想保持rewardViewModel
初始化以這樣一種方式,它是初始化一次每個cellForRowAtIndexPath
通話。像細胞的出列可能是。
如果我保留實例變量rewardViewModel
那麼它將僅初始化一次,並且在下一次迭代cellForRowAtIndexPath
中,同一個實例將被一次又一次地重複使用。
如果我刪除了實例變量並在cellForRowAtIndexPath
中設置了局部變量rewardViewModel
,那麼它每次都會是alloc init
,我不想這樣做。像下面這樣:
WARewardHistoryViewModel *rewardViewModel;
NSString *redeemRewardViewModelType = [[AWPViewFactory sharedInstance] getValue:@"WARewardHistoryViewModel"];
rewardViewModel = [[NSClassFromString(redeemRewardViewModelType) alloc] initWithReward:reward];
如何初始化我的指定的類爲cell的每次迭代一次?
我不是說這是一個乾淨的解決方案,但作爲一個poc的想法不會將模型屬性添加到您的細胞類解決問題? –
@ Kamil.S謝謝,但這將打破我的清潔細胞子類的目的。我已經完成了這個工作。 – Alok