美文网首页
TableView技巧

TableView技巧

作者: Vijay_ | 来源:发表于2017-10-04 02:30 被阅读4次

隐藏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<UITableViewRowAction *> *)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技巧

    添加索引条sectionIndex cell分割线 没有数据的cell不显示 高度自动计算 cell右侧小图标 t...

  • TableView技巧

    隐藏cell分割线_tableView.separatorStyle = NO; 让单元格之间有距离 设置分割线左...

  • 奥义-完美cell封装术

    导语: TableView的使用存在很多的技巧,如果能够巧妙的使用和封装TableView,代码的可读性和优美度会...

  • tableView 小技巧

    1.tableview section头部不悬停正常section不为0的时候,section的headerVie...

  • tableView小技巧

    重写scrollView最上方的偏移量(默认向下偏移64) if([selfrespondsToSelector:...

  • TableView小技巧

    在项目的时候,发现一个问题就是在Cell上的控件有背景色时,用户选择了这个cell,这个cell上控件的背景色,会...

  • tableView常用技巧

    cell 点击背景颜色 设置 (UITableViewCell *)tableView:(UITableView ...

  • tableView使用技巧

    UITableView 只更新一个cell 或某部分sectionhttps://www.jianshu.com/...

  • iOS 多选删除(附tableViewTips及单选删除)

    一、前言 这次分享并记录一下tableView的多选删除,并额外记录一下单选删除及tableView的设置小技巧。...

  • iOS 开发中常用的技巧

    iOS开发中常用但经常忘记的技巧 1、 隐藏tableViewCell的分割线:tableView.separat...

网友评论

      本文标题:TableView技巧

      本文链接:https://www.haomeiwen.com/subject/gdjqyxtx.html