UITableViewController使用

本文介绍iOS开发中列表视图控制器的基本使用方法,包括如何设置section和row的数量、定义cell的布局、设置辅助类型和视图等,并展示了不同配置下的实际效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

列表视图控制器,用起来很方便,不仅可以实现分组列表,连tem都有很多定义好的样式,使用时基本上不需要有大的自定义的部分,这里做一些简单的尝试

1.新建MyTableViewController的.h/.m文件
几个主要方法:

//注释:分组
//这里是说列表的section的个数,section就是分组;根据需要将列表分为几组;这里就1组,只会显示一个列表
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

//注释:每组的行数
//会用特殊的不同分组的行数不同,就在这里判断了;这里固定15行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 15;
}

//注释:添加每行布局
//这里处理每行要显示什么;通常布局是相同的,特殊情况会有不同行数的布局不同,主要在这里处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //根据cell定义的id,重复使用cell的布局
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myTableViewCell" forIndexPath:indexPath];
    // textview显示
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %ld",@"cell",(long)indexPath.row];

    return cell;
}

效果:
这里写图片描述

记住:
主要操作方法:section个数,row个数,row布局实现

2.把section个数改为2,row为3,试试,这里要设置section的头和脚的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

可以看到分组效果:
这里写图片描述

3.dequeueReusableCellWithIdentifier
TableVIew的实现原理是没有显示在屏幕上的item会被回收,当滚动时会使用旧的,所以要更新item的内容;在每个cellForRowAtIndexPath里使用dequeueReusableCellWithIdentifier

4.Cell的辅助类型:accessoryType,accessoryView

//1.右侧添加叹号的圆心按钮,可点击,即查看详情
cell.accessoryType = UITableViewCellAccessoryDetailButton;

效果:
这里写图片描述

//2.右侧添加箭头引导,点击图片无效果
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

效果:
这里写图片描述

//3.右侧圆心按钮+箭头,可点击
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

效果
这里写图片描述

//4.右侧添加对号,点击图片无效果
cell.accessoryType = UITableViewCellAccessoryCheckmark;

效果:
这里写图片描述

//自定义视图
UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_download"]];
cell.accessoryView =iv;

效果:
这里写图片描述

5.设置每组标题,尾部说明,右侧索引

#pragma mark 返回每组头标题名称
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    NSString *str = [NSString stringWithFormat:@"section %ld",(long)section];
    return str;
}

#pragma mark 返回每组尾部说明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    NSString *str = [NSString stringWithFormat:@"end %ld",(long)section];
    return str;
}

#pragma mark 返回每组标题索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    int num = tableView.numberOfSections;
    NSMutableArray *indexs = [[NSMutableArray alloc] init];
    for (int i=0; i<num; i++) {
        NSString *str = [NSString stringWithFormat:@"%d",i];
        [indexs addObject:str];
    }
    return indexs;
}

效果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值