iOS中tableView常用属性设置

在开发过程用遇到过的tableView的一些常用属性设置,做个总结。

一、tableView的属性

1.取消cell的分割线

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

2.取消tableView右侧的滚动条

tableView.showsVerticalScrollIndicator = NO;

3.当tableview数据较少时,动态隐藏tableView的底部线条

tableView.tableFooterView = [[UIView alloc]init];

4.设置tableView的偏移量

[myTableView setContentOffset:CGPointMake(0, 100) animated:YES];

5.隐藏tableView的footerView

tableView.sectionFooterHeight = 0;

6.设置tableView中cell的分割线左边距距离

if ([_tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }    //初始化tableView时设置

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

7.tableView选中时反选

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

8.在tableView索引中显示搜索按钮

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *arr = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
    [arr addObject:@"{search}"];//等价于[arr addObject:UITableViewIndexSearch];
    return arr;
}

二、Cell的属性

1.设置单元格选中时的背景色

方法一、

UIImageView *imageView = [UIImageView alloc]init];
imageView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = imageView;

方法二、

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];

2.设置单元格默认背景色

通过属性设置

cell.contentView.backgroundColor = [UIColor redColor];

通过方法设置

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  {
    cell.backgroundColor = [UIColor redColor];
}

3.取消单元格选中时背景色

cell.textLabel.highlightedTextColor = [UIColor redColor];

4.调整单元格之间的距离

- (void)setFrame:(CGRect)frame
{
    // tableView边框的宽度 #define kTableBorderWidth 5
    // 更改x、宽度
    frame.origin.x = kTableBorderWidth;
    frame.size.width -= kTableBorderWidth * 2;
    // 更改顶部间距、每个cell之间的间距
    frame.origin.y += kTableTopBorderWidth;
    frame.size.height -= kTableViewCellMargin;
    [super setFrame:frame]; 
}

5.单元格的属性

cell.accessoryType =UITableViewCellAccessoryNone;//cell没有任何的样式
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素;
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button;
cell.accessoryType =UITableViewCellAccessoryCheckmark;//cell右边的形状是对号;
cell.selectionStyle =UITableViewCellSelectionStyleNone;//cell选中状态的样式,为枚举类型
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值