UITableViewController使用

来源:互联网 发布:没有网络又可以玩游戏 编辑:程序博客网 时间:2024/06/05 08:06

列表视图控制器,用起来很方便,不仅可以实现分组列表,连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;}

效果:
这里写图片描述

0 0
原创粉丝点击