在一個(gè)項(xiàng)目中笆包,使用最多的還要數(shù)tableView环揽,更有甚者幾乎每個(gè)界面都是tableView,那么多協(xié)議方法,如果每個(gè)界面都寫一遍,是不是會(huì)很惡心灭贷,造成大量的代碼冗余。索性昧狮、封裝起來,調(diào)用起來非常方便、代碼看起來簡(jiǎn)潔,干凈衡创。
上圖:
![UITableView的封裝](http://upload-images.jianshu.io/upload_images/2381595-cc9de9367d9dd969.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
UITableView的封裝
1、不在繼承UITableVIewCell晶通,改成繼承自BaseTableViewCell
@interface ShopCell : BaseTableViewCell
@property(nonatomic,strong)GoodsModel *goodsData;
@property(nonatomic,weak)id<GoodsEditeDelegate>delegate;
@end
2、cell的賦值操作
- (void)setCellData:(id)item atIndexPath:(NSIndexPath *)indexPath {
GoodsModel *goodsData = (GoodsModel *)item;
哟玷、狮辽、、
}
3巢寡、Controller里面
1>定義一個(gè)tableView喉脖,注意是:BaseTableView
@property(nonatomic,strong)BaseTableView *shopTableView;
2>初始化tableView,寫各種代理方法
- (BaseTableView *)shopTableView {
if (!_shopTableView) {
_shopTableView = [[BaseTableView alloc] init];
// 加載的cell
_shopTableView.tableViewCellClass = [ShopCell class];
_shopTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_shopTableView.clearSeperatorInset = YES;
_shopTableView.isHeightCache = YES;
_shopTableView.showsVerticalScrollIndicator = NO;
WS(weakSelf);
// 可以理解為cell的復(fù)用池
_shopTableView.cellConfigureBlock = ^(UITableViewCell *cell, id item, NSIndexPath *indexPath) {
((ShopCell *)cell).delegate = weakSelf;
};
// 點(diǎn)擊cell跳轉(zhuǎn) (didSelectRowAtIndexPath)
_shopTableView.cellSelectBlock = ^ (UITableView *tableView, NSIndexPath *indexPath) {
// GoodsDetailViewController *detailVC = [[GoodsDetailViewController alloc] init];
// detailVC.goodsData = weakSelf.shopTableView.dataArray[indexPath.row];
// [AppCommon pushViewController:detailVC animated:YES];
};
// 側(cè)滑 (可添加多個(gè)抑月,寫到return返回的數(shù)組即可)
_shopTableView.editActionsForRowAtIndexPath = ^ (UITableView *tableView, NSIndexPath *indexPath) {
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// AlertViewController :我自己封裝的:http://www.reibang.com/p/418fe079d356
// ComAlertView *alertView = [[ComAlertView alloc]initWithTitle:@"刪除商品" message:@"確定要?jiǎng)h除寶貝嗎树叽?刪除之后無法修改了哦~" sureBtn:@"確定" cancleBtn:@"取消"];
// alertView.resultIndex = ^(NSInteger index){
// weakSelf.currentIndexPath = indexPath;
// [weakSelf.deleteCartRequest loadDataWithHUDOnView:nil];
// };
// [alertView showAlertView];
}];
deleteAction.backgroundColor = [UIColor redColor];
return @[deleteAction];
};
}
return _shopTableView;
}
4、將數(shù)據(jù)源谦絮,賦值給tableView的數(shù)據(jù)源
#pragma mark - 此demo题诵,由于是假數(shù)據(jù),所以當(dāng)真實(shí)數(shù)據(jù)的情況下层皱,不要忘了[tableView reload]操作性锭。
- (void)loadData {
GoodsModel *goodsData = [[GoodsModel alloc] init];
goodsData.name = @" Title";
goodsData.img_url = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1490339743727&di=921466b975276b5abc5752d8ca088529&imgtype=0&src=http%3A%2F%2Ff.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F80cb39dbb6fd526695cb9cc4a918972bd5073679.jpg";
self.shopTableView.dataArray = [@[goodsData,goodsData,goodsData,goodsData,goodsData,goodsData] mutableCopy];
}
哪里有不合適的地方,歡迎各位大神指正叫胖,謝謝草冈。
喜歡的小伙伴快去下載吧,
代碼:https://github.com/Baiyongyu/UITableView---.git
感覺不錯(cuò)的小伙伴記得來個(gè)star哈瓮增。_