UIday0901:UITableView 1 表视图的属性和用法 1

来源:互联网 发布:淘宝助理怎样下载宝贝 编辑:程序博客网 时间:2024/06/07 15:35

UITableView 表视图的属性和用法 1

一、UITableView 表视图

UITableView有两种风格:UITableViewStylePlain 和 UITableViewStyleGrouped

UITableView 继承⾃自 UIScrollView,所以可以滚动

表视图的每⼀条数据都是显⽰在 UITableViewCell对象中

表视图可以分区显⽰数据,每个分区称为⼀个section,每⼀⾏称为row,编号都是从0开始

表视图的属性:
     style样式:plain group
     分割线样式:separatorStyle
     分割线颜色:separatorColor
     行高:rowHeight

二、UITableViewCell

UITableViewCell:UITableView中每行数据都是一个UITableViewCell,在这个控件中为了显示更多的信息,iOS已经在其内部设置好了多个子控件以供开发者使用。如果我们查看UITableViewCell的声明文件可以发现在内部有一个UIView控件(contentView,作为其他元素的父控件)、两个UILable控件(textLabel、detailTextLabel)、一个UIImage控件(imageView),分别用于容器、显示内容、详情和图片。使用效果类似于微信、QQ信息列表。

注意:随着我们的应用越来越复杂,可能经常需要调试程序,在iOS中默认情况下不能定位到错误代码行,我们可以通过如下设置让程序定位到出错代码行:Show the Breakpoint  navigator—Add Exception breakpoint。

设置图⽚     imageView
     设置⽂本     textLabel
     指定选中效果     selectionStyle
     指定辅助效果样式     accessoryType      
UITableViewCell 创建UITableViewCell的辅助效果样式,系统自带有四种:

     UITableViewCellStyleDefault  //只显示图片和 主标题title
     UITableViewCellStyleValue1   //title和detailtitle 分左右显示  且显示图片
     UITableViewCellStyleValue2   //title和detailtitle 分左右靠左显示  不显示图片
     UITableViewCellStyleSubtitle    //title和detailtitle 靠左上下显示  且显示图片


// cell右侧图标显示样式,默认为无

    cell.accessoryType = UITableViewCellAccessoryNone;      //cell没有任何的样式;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;      //cell的右边有一个小箭头,距离右边有十几像素;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;      //cell右边有一个蓝色的圆形button;
    cell.accessoryType = UITableViewCellAccessoryCheckmark;      //cell右边的形状是对号;

    // 创建cell 第一个参数样式  第二个参数重用标识    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue2) reuseIdentifier:nil];    // 给cell赋值    cell.textLabel.text = @"测试数据";        // 设置图片    cell.imageView.image = [UIImage imageNamed:@"img01.jpg"];        // detailTextLabel 设置完之后运行没东西,原因是这个跟样式有关    cell.detailTextLabel.text = @"123456";    // 设置显示样式    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

三、使用UITableView 需要遵循两个代理:

<UITableViewDataSource,UITableViewDataSource>

UITableViewDataSource  存放跟数据相关的操作方法,其中有两个方法是必须实现的

UITableViewDataSource  存放跟显示样式相关的操作方法

#pragma mark  --tableView  dataSource// 控制每个分区中有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    NSLog(@"%ld",section);    return 5;    }// 创建cell,给cell赋值- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //当前显示过几条cell,就执行多少次。显示第几分区 第几个  length表示  path表示    NSLog(@"%@",indexPath);    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue2) reuseIdentifier:nil];    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];    return cell;}// 有多少个分区 常用功能- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{        // 1、先查询有多少个分区 (第一个从0开始)    return 8;}

四、UITableView 的重用机制

UITableView的重用机制可以大大节省系统的内存空间

UITableView靠mutableSet来实现重⽤功能

重用功能的实现方法:

// 创建cell,给cell赋值- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 只要cell显示就会走这个方法  解决这个问题有一个重用的概念     // 从重用池(重用队列)通过 identifier 拿cell mutableSet集来实现的    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];    if (cell == nil) {        //判断从重用池是否拿到cell,如果没有拿到,就创建。        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];    }        cell.textLabel.text = @"测试数据";           return cell;}

五、自定义区头区尾

// delegate:// - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;// - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;// - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;// - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

六、单元格的高度及选中

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;<pre name="code" class="objc">// 为每一个分区设置一个底部标题  用于解释分区作用-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    return @"版权所有";}// 生成一个索引  帮助用户快速查找到相应分区  用的最多的是通讯录-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{    // 分区号和数组索引一一对应    return @[@"a",@"b",@"c"];}

#pragma mark  ----delegate//设置分区header高度-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 50;}-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 50;}// 在header的位置  放置一个View-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];    button.backgroundColor = [UIColor redColor];    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];    return button;}-(void)buttonAction:(UIButton *)sender{    NSLog(@"123");}// 在foot的位置 放置一个button-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];    button.backgroundColor = [UIColor greenColor];    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];    return button;}// 设置每一行的高度-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 100;}// 这个方法非常重要  选中任一行就会执行这个方法  indexPath就是该行的索引-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%@",indexPath);}



设置背景色和横线:


_tableView.separatorColor = [UIColor clearColor];  //设置背景色为透明.


// 在storyBoard上设置tableVIew的代理

在这里设置,   就不需要再到.h或.m文件里写<UITableViewDelegate,UITableViewDataSource>







0 0
原创粉丝点击