UITableView常用设置

来源:互联网 发布:grpc javascript 编辑:程序博客网 时间:2024/06/05 15:22

1. 设置分割线长度为屏幕宽度

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {        [_tableView setLayoutMargins:UIEdgeInsetsZero];    }    else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){        [_tableView setSeparatorInset:UIEdgeInsetsZero];    }- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {        [cell setSeparatorInset:UIEdgeInsetsZero];        [cell setLayoutMargins:UIEdgeInsetsZero];    }    else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){        [cell setSeparatorInset:UIEdgeInsetsZero];    }}

2. 隐藏UITableView的滚动条以及修改滚动条的颜色

(1)隐藏

self.tableView.showsVerticalScrollIndicator = NO;

(2)修改颜色

self.tableView.indicatorStyle=UIScrollViewIndicatorStyleWhite;

3.设置没有内容的单元格不显示

_tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];

4.设置sectionheader自定义字体和颜色

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UIView *customView = [[[UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)] autorelease];    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];    headerLabel.backgroundColor = [UIColor clearColor];    headerLabel.textColor = [UIColor lightGrayColor];    headerLabel.font = [UIFont systemFontOfSize:14.0];    headerLabel.frame = CGRectMake(10, 0, 300, 44);    if (section == 0) {        headerLabel.text = NSLocalizedString(@"GroupName", @"群组名称");    }    else if (section == 1){        headerLabel.text = NSLocalizedString(@"GroupDescription", @"群组公告(0~36)");    }    [customView addSubview:headerLabel];    return customView;}

5.设置cell里面的字体不会有小部分超出view

cell.contentView.clipsToBounds = YES;cell.clipsToBounds = YES;

6.当TableView点击的时候,去除点击的阴影

我们在点击UITableView 的cell时,会出现阴影,如不做处理,就会一直显示,怎么样让它点击之后就消失呢?只要重写UITableView的代理方法,就可以解决,方式如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //当手指离开某行时,就让某行的选中状态消失    [tableView deselectRowAtIndexPath:indexPath animated:YES];}

7.设置分割线距周围的距离

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {        //分别为top, left, bottom,right        cell.separatorInset = UIEdgeInsetsMake(0, 10, 0, 0);    }
原创粉丝点击