iOS之表视图控制器UITableViewController

UITableView

表视图

简述:表视图UITbaleView是iOS中最经常使用的的视图,一般用来管理一组具备相同数据结构的数据。UITableView继承自UIScrollView,能够滚动,视图中每一条数据都是显示在UITableViewCell对象中的,表视图能够分区显示数据,每一个分区称为一个section,每一行称为row,编号从0开始.

(1)建立web

UITableView *tableView = [UITableView alloc]initWithframe:
style:
style有两种形式:
(1)UITableViewStyleplain  分区之间有间隔
(2)UITableViewStyleGrouped   分区之间没有间隔条

(2)基本属性数据结构

分割线样式:separatorStyle
分割线颜色:separetouColor
行高    :rowHeight

(3)数据源DataSource 须要给tableView指定一个数据源,能够给tableView提供数据,首先遵照协议,实现协议里面必须实现的两个方法:ide

每个分区的单元格数:
- (NSInteger)tableView:(UITableView *)tableview
    numberOfRowsInSection:(NSIndexPath *)indexPath
加载单元格:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath:)indexPath;

(4)cell 分区每个单元格称为cell,系统提供了4种style的cell(UITAbleViewCellStyle...),不一样的样式在包含的控件布局略有不一样 (5)表头,表尾函数

能够给表头tableview.tableHeaderView ,表尾
tableview.tableFootview自定义视图。
注意⚠!!!表头,表尾不能为同一对象

(6)设置分区数布局

-(NSInteger)numberOfSectionsInTableView:(UITableView *)
tablview//默认返回值是1

(7)设置分区标题spa

-(NSString *)tableView:(UITableView *)tableview 
titleForheaderInSection:(NSInteger)section

(8)设置分区索引code

-(NSArray *)sectionIndexTitleForTableview:(UITableView *)tableview

tableView重用机制

UITableView靠mutableSet来实现重用功能的,出屏幕的cell会被添加到mutableSet中,进去屏幕的cell, 先从set中获取,若是获取不到,才建立一个cell,在cell显示以前,给cell赋上相应的内容。对象

体如今cell(单元格)的加载继承

-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static *identifier = @"cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//从个重用队列里面取
     if (cell == nil) {//若是取出来为空则从新建立
    cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:identifier];
}
return cell;
}

UITableView编辑

增删

(1)设置tablView处于可编辑状态索引

-(void)setEditing:(BOOL)editing animated:(BOOL)animated

(2)设置哪些行能够被编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

(3)返回编辑样式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

(4)增删操做

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
[tableView beginupdates];开始编辑
if(editingStyle== UITableViewCellEditingStyleDelete)
{
    删除操做
}else
{
    增价操做
}
[tableView endupdates];结束编辑

移动

(1)让tableView处于可编辑状态

(2)指定几区几行可被移动

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

(3)当出现越区移动时候触发

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

被选中的时候触发

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

表视图控制器

UITableViewController

新建的UITableViewController只是简单的把上面的函数自动生成,不用咱们一个一个去找,并且它的self.view是一个UITableView类型的,因此不用再新建UITableView,默认容许编辑。编辑样式默认是所有删除

自定义cell

自定义cell就是建立一个UITableViewCell子类,将cell上的控件建立都封装在子类中

!!! 子视图控件添加到cell的contentView上 main

如何通讯?

cell中声明一个Model类型的属性,viewController中获取到Model对象后赋值给cell的Model属性,

cell中重写Model的setter方法,把Model对象中的内容从新赋值给各个控件。(M和V不直接通讯,C负责M和V之间进行通讯)

好比:student是一个model,也是自定义cell里面中的一个变量,在viewController里面获取到model对象赋值给cell的Model属性,则会进行下边的操做

-(void)setStudent:(Student *)student
{
if (_student != student) {
    [_student release];
    _student = [student retain];
    //        同时给内部控件赋值
    _nameLabel.text = student.name;
    _phoneNumberLabel.text = student.phoneNumber;
    _sexLabel.text = student.sex;
    _introduceLabel.text = student.introduce;
    _iconView.image = [UIImage imageNamed:student.icon];
    //        改变iconView的高度,
    CGRect rec = _introduceLabel.frame;
    rec = CGRectMake(rec.origin.x, rec.origin.y, rec.size.width, [BoyTableViewCell heightForString:student.introduce ]);
    _introduceLabel.frame = rec;
}
}

多种cell混合使用

在开发中比较常见一个tableView里面有多种不一样布局样式的cell,所以多种类型的cell混合在一个tableView中使用的状况非常常见。因此常常自定义多种不一样类型的cell。

注意事项

一般咱们在tableView :cellForRowAtIndexPath:方法中根据不一样的Model来决定使用什么类型的cell,每种类型的cell要定义不一样的重用标识符以存储在不一样的重用队列中。这样cell重用的时候会根据重用标识从重用队列中取出哪一种类型的cell

cell自适应高度

tableView:heightForRowAtindexPath:方法要比tableView:cellForRowAtIndexPath先执行,因此应该提早计算好每行cell须要多少高度

相关文章
相关标签/搜索