導(dǎo)語(yǔ):
通常我們?cè)陂_(kāi)發(fā)中,90%的時(shí)間都是在寫(xiě)一些重復(fù)的垃圾代碼询一,真正的寫(xiě)代碼時(shí)間其實(shí)只有5%左右的時(shí)間悠夯,而tableView卻是我們最常用的控件,也是復(fù)用率最高的控件竟稳,那么簡(jiǎn)化tableView代碼就顯得尤為重要新翎。
簡(jiǎn)化tableView在控制器VC中的代碼
而通常我們最直接的簡(jiǎn)化代碼操作是繼承父類(lèi),把公共代碼放在父類(lèi)控制器中住练,這是最簡(jiǎn)單也是最有效的地啰。還有一種是寫(xiě)一個(gè)分類(lèi),在分類(lèi)中寫(xiě)公共方法讲逛,這在寫(xiě)tableView是將會(huì)用到亏吝。
通常在寫(xiě)VC代碼時(shí),自定義各種cell,避免不了重復(fù)無(wú)休止的寫(xiě)一些沒(méi)有任何營(yíng)養(yǎng)的代碼 如
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DiscoveryMenuContentView *cell = [tableView dequeueReusableCellWithIdentifier:[DiscoveryMenuContentView identifier] forIndexPath:indexPath];
WS(weakSelf)
cell.infoModelList = self.disoveryModel.infoList;
return cell;
}
這將浪費(fèi)了大量的時(shí)間去寫(xiě)相似代碼盏混,而且代碼重用率非常低蔚鸥,耦合性太高。所以我們這里需要將能抽取出來(lái)的代碼全部都抽取出來(lái)许赃。
只需要寫(xiě)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [tableView dequeueReusableCellAndLoadDataWithAdapter:self.dataArray[indexPath.row] delegate:self indexPath:indexPath];
}
就能夠使用于任何場(chǎng)景止喷,而可以將這段代碼寫(xiě)到父類(lèi)中 所有的子類(lèi)控制器只需要繼承或者重寫(xiě)自定義即可。
而其實(shí)這段代碼只是寫(xiě)在了tableView的分類(lèi)中
@implementation UITableView (CustomCell)
- (CustomCell *)dequeueReusableCellAndLoadDataWithAdapter:(CellDataAdapter *)adapter indexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [self dequeueReusableCellWithIdentifier:adapter.cellReuseIdentifier forIndexPath:indexPath];
[cell loadContentWithAdapter:adapter delegate:nil tableView:self indexPath:indexPath];
return cell;
}
- (CustomCell *)dequeueReusableCellAndLoadDataWithAdapter:(CellDataAdapter *)adapter
delegate:(id <CustomCellDelegate>)delegate
indexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [self dequeueReusableCellWithIdentifier:adapter.cellReuseIdentifier forIndexPath:indexPath];
[cell loadContentWithAdapter:adapter delegate:delegate tableView:self indexPath:indexPath];
return cell;
}
抽取出來(lái)的只是dequeue的代碼及數(shù)據(jù)的一些賦值代碼混聊,但是卻大大提高了效率
賦值操作則是寫(xiě)在了adapter文件中(即分類(lèi)中的CellDataAdapter)弹谁,adapter文件之抽取出來(lái)的是一個(gè)抽象類(lèi),當(dāng)中有cell的賦值調(diào)用句喜,cell數(shù)據(jù)源及cell高度等緩存一些操作预愤。
而adapter是封裝了數(shù)據(jù)model的一個(gè)類(lèi),可以像添加model一樣添加到數(shù)據(jù)源數(shù)組中
+ (CellDataAdapter *)cellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifiers data:(id)data
cellHeight:(CGFloat)cellHeight cellType:(NSInteger)cellType {
CellDataAdapter *adapter = [[self class] new];
adapter.cellReuseIdentifier = cellReuseIdentifiers;
adapter.data = data;
adapter.cellHeight = cellHeight;
adapter.cellType = cellType;
return adapter;
}
+ (CellDataAdapter *)cellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifiers data:(id)data
cellHeight:(CGFloat)cellHeight cellWidth:(CGFloat)cellWidth
cellType:(NSInteger)cellType {
CellDataAdapter *adapter = [[self class] new];
adapter.cellReuseIdentifier = cellReuseIdentifiers;
adapter.data = data;
adapter.cellHeight = cellHeight;
adapter.cellWidth = cellWidth;
adapter.cellType = cellType;
return adapter;
}
我們?cè)谡{(diào)用的時(shí)候是可以直接賦值的
如
for (int i=0;i<result.count;i++)
{
[self.dataArray addObject:[CustomCell dataAdapterWithData:result[i]]];
}
在繼承自CustomCell的類(lèi)中直接可以賦值操作咳胃,大大簡(jiǎn)化了代碼植康,解耦。在中小型及多table項(xiàng)目中尤為方便展懈。