TableView技巧

隐藏cell分割线
_tableView.separatorStyle = NO;

让单元格之间有距离

//使用分组风格的table
// _tableView = [[UITableView alloc] initWithFrame:CGRectMake(12, 0, bounds.size.width - 24, bounds.size.height) style:UITableViewStyleGrouped];
//用组作为单元格  每一组就一个单元格 用height和footer的高度做间隔
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    NSLog(@"%@",cell);
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
        cell.layer.cornerRadius = 6;
        cell.layer.masksToBounds = YES;
    }
    
    cell.textLabel.text = @"hello";
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  10;
}
//如果没有值或者0系统就会默认设置
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}


设置分割线左对齐

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
//    判断是否有该方法
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
//    防止继承表格的边距
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

单元格编辑按钮

//滑动显示编辑按钮
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    }
//自定义多个编辑按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction* action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"删除了");
    }];
    UITableViewRowAction* action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"查看更多" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"更多了");
    }];
    return @[action1,action2];
}

单元格系统提供的样式

  1. Default : 左边一个图片 左边一个titleLabel **
  2. Value1 : 左边一个图片 左边一个titleLabel 右边一个灰色detailLabel
  3. Value2: 左边一个蓝色titleLabel 右边一个黑色detailLabel
  4. Subtitle: 左边一个图片 左边一个上面titleLabel 下面detailLabel **

cell.accessoryType设置单元格右边辅助视图 系统提供的小图标 小三角等小标识符图标

cell.accessoryView设置单元格右边辅助视图 自定义视图

你可能感兴趣的:(TableView技巧)