StoryBoard中tableView中cell的各个属性说明

来源:互联网 发布:数据库查询语句大全 编辑:程序博客网 时间:2024/05/19 16:47

表视图(UITableView)与表视图控制器(UITableViewController)其实是一回事。 表视图控制器是一种只能显示表视图的标准视图控制器,可在表视图占据整个视图时使用这种控制器。虽然如此,相对于使用标准视图控制器并自行添加表视图,使用表视图控制器除了将自动设置委托和数据源属性外,没有任何其它的优势。

对于表视图,最基本的设置是Content(内容)属性,它包含两个值:Static Cells和Dynamic Prototypes。Static Cells用来显示固定的单元格,内容呈现主要通过Xcode的可视化编程来实现,不需要额外的代码支持。Dynamic Prototypes为动态单元格,通过设定一个Cell模板,然后通过实现datasource接口和delegate接口的一些关键方法,从而动态生成表视图。

需要注意的是,Static Cells模式仅仅适用于表视图控制器(UITableViewController),当你尝试设置标准视图控制器下的表视图(UITableView)content为Static Cells时,Xcode将提示一个错误:Static table views are only valid when embedded in UITableViewController instances。

表视图有两种外观(Style):Plain和Grouped。下图演示了这两种样式的差异:

Plain:

Grouped:

Separator属性用于指定分区之间的分隔线外观,它包含两个下拉列表,一个用来设置分割线的外观,有3个值:None(无分割线)、Single Line(单线条)、Single Line Etched(带浮雕效果的线条);另一个用来设置线条的颜色。

Selection、Editing以及Show Selection on Touch复选框用于设置表格被用户触摸时的行为。

Index Row Limit属性与UITableView的sectionIndexMinimumDisplayRowCount有关,这个属性的含义是:"当行数达到某个数值,则显示索引栏"。(索引栏是通过sectionIndexTitlesForTableView方法来实现的)

每个单元格(Cell)都有独特的标识符。这种标示符被称为重用标识符(reuse identifier),用于在编码时引用单元格;例如在Dynamic Prototypes模式下,可以指定多个Cell模板,为每个模板命名不同的标示符,然后根据情况调用不同的Cell模板。

单元格的Style属性有下列五种:

Custom -- 用户自定义
Basic -- 只包含一个Title
Right Detail --包含一个Title,并且在右侧显示Detail
Left Detail -- 包含一个Title,并且在左侧显示Detail
Subtitle -- 包含一个Title,并且在Title的下方显示Detail

Basic、Right Detail和Subtitle有一个Image属性,可以选择任意一张图片来添加图像,这里只是用作占位。

下拉列表Selection用于设置单元格被选中时的颜色。只有三个值供选择:None、Blue、Gray。

下拉列表Accessory用于设置单元格右边的附属图形(通常是展开箭头)。有四个值:None、Disclosure Indicator、Detail Disclosure、Checkmark。

Indentation设置栏包括Level和Width输入框,分别对应indentationLevel(缩进等级)和indentationWidth(缩进宽度),最终的索引值=缩进等级*缩进宽度。缩进等级默认值为0(没有缩进),缩进宽度默认值为10points。一般不直接设置缩进等级,而是根据行索引返回值:

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{    NSUInteger row = [indexPath row];    return row;}

Indent while editing复选框应该是“编辑状态下缩进”(取消选择,编辑状态下可能就不缩进了,留待以后验证)。

Shows Re-order Controls复选框应该是“显示Re-order Controls",Re-order Controls是编辑状态下出现的一系列控件。

要想让表视图正常工作,需要遵守两个协议--UITableViewDataSource与UITableViewDelegate。

UITableViewDataSource(数据源协议)需要实现的主要方法:

numberOfSectionsInTableView: -- 返回表视图的分区数。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    //返回了2个分区    return 2;}

tableView:numberOfRowsInSection: -- 返回给定分区包含多少行。分区索引从0开始。

复制代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    //第一个分区返回2行,第二个分区返回5行    if(section==0)    {        return 3;    }    else if(section==1)    {        return 5;    }    return 0;}
复制代码

tableView:titleForHeaderInSection: -- 返回一个字符串,用于给定分区的标题。

复制代码
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    //第一个分区标题为"亚洲",第二个分区标题为"欧洲"    if(section==0)    {        return @"亚洲";    }    else    {        return @"欧洲";    }}
复制代码

tableView:cellForRowAtIndexPath: -- 返回一个单元格对象,用于显示在表视图的指定位置。

复制代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"mycell"];        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];            if(indexPath.section==0)//第一个分区    {        if(indexPath.row==0)        {            cell.textLabel.text = @"第一分区第一行";            cell.detailTextLabel.text = @"section1 row1";            cell.imageView.image = [UIImage imageNamed:@"s1r1.gif"];        }        else        {            cell.textLabel.text = @"第一分区其它行";            cell.detailTextLabel.text = @"section1 rowX";            cell.imageView.image = [UIImage imageNamed:@"s1rx.gif"];        }    }    else//其它分区    {        cell.textLabel.text = @"其它分区其它行";        cell.detailTextLabel.text = @"sectionX rowX";        cell.imageView.image = [UIImage imageNamed:@"sxrx.gif"];    }    return cell;}
复制代码

UITableViewDelegate(委托协议)主要响应用户在表视图进行的操作,最基础的是用户触摸单元格:

tableView:didSelectRowAtIndexPath: 与 tableView:didDeselectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"you selected section %d row %d",indexPath.section,indexPath.row);}

0 0