導(dǎo)語:
通常我們?cè)陂_發(fā)中,90%的時(shí)間都是在寫一些重復(fù)的垃圾代碼,真正的寫代碼時(shí)間其實(shí)只有5%左右的時(shí)間,而tableView卻是我們最常用的控件,也是復(fù)用率最高的控件,那么簡化tableView代碼就顯得尤為重要。
簡化tableView在控制器VC中的代碼
而通常我們最直接的簡化代碼操作是繼承父類,把公共代碼放在父類控制器中,這是最簡單也是最有效的。還有一種是寫一個(gè)分類,在分類中寫公共方法,這在寫tableView是將會(huì)用到。
通常在寫VC代碼時(shí),自定義各種cell,避免不了重復(fù)無休止的寫一些沒有任何營養(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í)間去寫相似代碼,而且代碼重用率非常低,耦合性太高。所以我們這里需要將能抽取出來的代碼全部都抽取出來。
只需要寫
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [tableView dequeueReusableCellAndLoadDataWithAdapter:self.dataArray[indexPath.row] delegate:self indexPath:indexPath];
}
就能夠使用于任何場景,而可以將這段代碼寫到父類中 所有的子類控制器只需要繼承或者重寫自定義即可。
而其實(shí)這段代碼只是寫在了tableView的分類中
@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;
}
抽取出來的只是dequeue的代碼及數(shù)據(jù)的一些賦值代碼,但是卻大大提高了效率
賦值操作則是寫在了adapter文件中(即分類中的CellDataAdapter),adapter文件之抽取出來的是一個(gè)抽象類,當(dāng)中有cell的賦值調(diào)用,cell數(shù)據(jù)源及cell高度等緩存一些操作。
而adapter是封裝了數(shù)據(jù)model的一個(gè)類,可以像添加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的類中直接可以賦值操作,大大簡化了代碼,解耦。在中小型及多table項(xiàng)目中尤為方便。