UITableView 常用属性及方法

在上一篇文章的基础上, 从两个方面来对创建的TabelView进行样式设计, start....
<ol>
<li>tableView的基本属性

//设置行高
tableView.rowHeight = 50;

//设置分割线颜色
tableView.separatorColor = [UIColor redColor];

//设置分割线样式
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

//设置头部视图( # 整个tabelView的头视图)
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
headerView.backgroundColor = [UIColor yellowColor];
tableView.tableHeaderView = headerView;

//设置底部视图
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
footerView.backgroundColor = [UIColor blackColor];
tableView.tableFooterView = footerView;```


<li>tableView的常用方法

tableView必须实现的代理方法

//1: 设置每个分区内的row的值

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
    }

//2: 返回cell

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //根据重用池标识去取重用池的cell, 在viewDidLoad方法中注册cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //设置cell上显示的文本
    cell.textLabel.text = [NSString stringWithFormat:@"row: %ld", indexPath.row];
    //返回cell
    return cell;
    }

tableView常用的普通方法

//1. 设置每一行cell的高度(同rowHeight, 但是该方法可用来设置单个row的高度)

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //我们可以根据row指定某一个cell的具体高度
    if (indexPath.row == 0) {
    return 100;
    }
    return 50;
    }

//2. 设置分区个数(默认为1)

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
    }

//3. 设置右侧的索引
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"I", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
}

//4. 设置头部标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"start........";
}

//5. 设置每一个区头部高度

  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 100;
    }

//6. 设置尾部标题

  • (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"ending........";
    }

//7. 设置每一个区尾部高度

  • (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
    }

//8. 当点击某个cell触发的方法

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //这个方法, 是当我们单击cell的某一行的时候, 要实现哪些事件, 事件写在该方法里就可以.

//9. 每个分区的头部视图

  • (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

//10. 每个分区的尾部视图

  • (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
    }```

如图:界面各部分属性和设置对应如下

t.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容