1. 初始化
#pragma mark - Getter- (UITableView *)myTableView { if (!_myTableView) { _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _myTableView = [UITableView newAutoLayoutView]; _myTableView.delegate = self; _myTableView.dataSource = self; [self.view addSubview:_myTableView]; } return _myTableView;}
2. 设置UITableView的行为
- (void)initRootView { self.edgesForExtendedLayout = UIRectEdgeNone; self.view.backgroundColor = kColor(0xeeeeee); //去掉多余的分割线 self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.myTableView.backgroundColor = kColor(0xeeeeee); self.myTableView.tableFooterView = [UIView new]; //设置cell高度自适应 不用计算cell高度了 self.myTableView.rowHeight = UITableViewAutomaticDimension; self.myTableView.estimatedRowHeight = 44; //添加下拉刷新 上拉加载 用到 MJRefresh [self addHeaderFooterView];}// 添加下拉刷新头部控件- (void)addHeaderFooterView { __unsafe_unretained typeof(self) vc = self; MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [vc refreshRequest]; }]; header.lastUpdatedTimeLabel.hidden = YES; header.stateLabel.textColor = kColor(0x888888); self.myTableView.mj_header = header; //自动刷新(一进入程序就下拉刷新) [self.myTableView.mj_header beginRefreshing]; // 添加上拉刷新尾部控件 MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{ [vc loadMoreRequest]; }]; footer.stateLabel.textColor = kColor(0x888888); self.myTableView.mj_footer = footer; self.myTableView.mj_footer.hidden = YES;}//继承vc 重写上下拉方法调用接口- (void)refreshRequest {}- (void)loadMoreRequest {}//某些页面不需要上下拉 重新new个UITableView 覆盖initRootView方法- (void)rootTableViewWithoutRefresh { self.myTableView = [UITableView newAutoLayoutView]; self.myTableView.delegate = self; self.myTableView.dataSource = self; self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.myTableView.backgroundColor = kColor(0xeeeeee); self.myTableView.tableHeaderView = [UIView new]; self.myTableView.tableFooterView = [UIView new]; self.myTableView.rowHeight = UITableViewAutomaticDimension; self.myTableView.estimatedRowHeight = 44; [self.view addSubview:self.myTableView];}
3. 注册自定义cell
[self.myTableView registerClass:[AuctionNoticeCellAll class] forCellReuseIdentifier:@"AuctionNoticeCellAll"];
4. 复用cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { AuctionNoticeCellAll *cell = [self.myTableView dequeueReusableCellWithIdentifier:@"AuctionNoticeCellAll" forIndexPath:indexPath]; //item是mode [cell setupItem:item]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; }
5. 右滑删除cell
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES;}- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete;}//自定义删除view- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *delAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { //删除 [self.noticeAlarms removeObjectAtIndex:indexPath.section]; [self.myTableView beginUpdates]; [self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; [self.myTableView endUpdates]; }]; delAction.backgroundColor = kColorRed; return @[delAction];}/** * 删除显示的view */- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"normal" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { //删除 [self.items removeObjectAtIndex:indexPath.section]; [self.myTableView beginUpdates]; [self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop]; [self.myTableView endUpdates]; }]; UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"default" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop]; }]; UITableViewRowAction *action3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"destructive" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom]; }]; action1.backgroundColor = [UIColor lightGrayColor]; action2.backgroundColor = [UIColor greenColor]; action3.backgroundColor = [UIColor redColor]; action1.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];// action2.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];// action3.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]; if (indexPath.row%3 == 0) { return @[action1, action2]; } return @[action1, action2, action3];}
6. tip
//tableview 删除某行 先删除数据在调用deleteRowsAtIndexPaths:方法前,要确保数据为最新。也就是说,先将要删除的数据从数据源中删除 [self.rootItems removeObjectAtIndex:indexPath.row]; [self.myTableView beginUpdates]; [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.myTableView endUpdates];删除section[self.noticeAlarms removeObjectAtIndex:indexPath.section]; [self.myTableView beginUpdates]; [self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft]; [self.myTableView endUpdates];
7. 滚动时显示的cell由小变大动画
在tableView:cellForRowAtIndexPath:设置初始值 在willDisplayCell恢复[cell.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { obj.transform = CGAffineTransformMakeScale(.8, .8); }];/** * cellForRowAtIndexPath初始化cell后准备显示cell调用. 不需要修改cell.transform */- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { [UIView animateWithDuration:.5 animations:^{ [cell.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { obj.transform = CGAffineTransformIdentity; }]; NSLog(@"willDisplayCell %@", cell); }];}
8.
9.
10.
11.