##DAY10 UITableView基础

##DAY10 UITableView基础

UITableView继承于UIScrollView,可以滚动。

UITableView的每⼀条数据对应的单元格叫做Cell,是UITableViewCell的⼀个对象,继承于UIView。

UITableView可以分区显⽰,每⼀个分区称为section, 每⼀⾏称为row, 编号都从0开始。

系统提供了⼀个专门的类来整合section和row,叫做NSIndexPath。

#pragma mark ———————UITableView基本属性——————————

创建:

initWithFrame:style:

style的两种类型:

UITableViewStylePlain

UITableViewStyleGrouped

UITableView的属性:

rowHeight 设置行高,只能设置全部行高相同的情况

separatorColor 设置分割线的颜色

separatorStyle 设置分割线的风格

tableHeaderView 设置表格头部控件

tableFooterView 设置尾部控件

dataSource 显示数据相关的代理

delegate 视图操作相关的代理

#pragma mark --------UITableViewDataSource----------

tableView:numberOfRowsInSection: 每一组的行数,可以用switch(section)对不同的组进行判断

tableView:cellForRowAtIndexPath: 设置每一行显示怎样的内容(cell),每当有一个cell进入视野范围内,就会调用

numberOfSectionsInTableView:设置组数,不写实现,默认是一组

tableView:titleForHeaderInSection:第section组显示怎样的头部标题,概括这一组是干什么用的

tableView:titleForFooterInSection:第section组显示怎样的尾部标题,详细描述这一组是干什么用的

注意:TA有固定的字体样式。如果你想要不同的东西,使用自定义视图(UILabel)

tableView:canEditRowAtIndexPath: 允许被编辑的行,如果没有实现,所有行被认为是可编辑的。tableView:canMoveRowAtIndexPath: 允许重新排序,默认情况下全部可以

sectionIndexTitlesForTableView: 右侧边栏的索引(e.g. "ABCD...Z#")

tableView:sectionForSectionIndexTitle:atIndex: 设置一个组所对应的标题或索引 (e.g. "B",1)

tableView:commitEditingStyle:forRowAtIndexPath:提交编辑的具体实现

tableView:moveRowAtIndexPath:toIndexPath: 移动行的具体实现

#pragma mark --------UITableViewDelegate----------

点击单元格后触发的方法,跳转、传值都在这个方法中完成

tableView:didSelectRowAtIndexPath:

可变高度

tableView:heightForRowAtIndexPath:

tableView:heightForHeaderInSection:

tableView:heightForFooterInSection:

设置每一组的头视图和尾视图,注意高度,和内存管理autorelease

tableView:viewForHeaderInSection:

tableView:viewForFooterInSection:

其他方法见头文件和API

#pragma mark --------UITableViewCell----------

UITableViewCell的属性:

imageView 图片视图

textLabel 设置cell显示的文字

detailTextLabel 副标题视图

accessoryType 设置右边指示器的类型,下面这个枚举是个 >

accessoryView 自定义附属视图

backgroundView 设置未选中时的背景(背景的view,用init创建,不设置frame)

backgroundColor backgroundView的优先级高于backgroundColor,且前者可设置被选中的状态,后者不可

selectedBackgroundView 设置被选中时的背景

selectionStyle 选中的行的显示风格

cell的创建方法:

initWithStyle:reuseIdentifier:

#pragma mark --------UITableViewCell的重用机制----------

cell的创建方法:

initWithStyle:reuseIdentifier:

tableView的方法:

表视图通过重用标识去重用池中查找是否有能够被重用的cell

dequeueReusableCellWithIdentifier: (forIndexPath:)注意后半截在XIB时用

#pragma mark ------读取数据------

//读取数据

- (void)handleData{

    //获取到plist文件在包中的路径

    NSString *filePlath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];

    //从路径中得到一个数组

    NSArray *arr = [NSArray arrayWithContentsOfFile:filePlath];

    //初始化模型数组

    _modelArr = [[NSMutableArray alloc] initWithCapacity:0];

    for (NSDictionary *dic in arr) {

        Student *stu = [[Student alloc] init];

        [stu setValuesForKeysWithDictionary:dic];

        [_modelArr addObject:stu];

        [stu release];

    }

}

#pragma mark ——————————————建立模型——————————————

异常处理:

1、当使用KVC为模型赋值时,如果模型内部,没有声明相对应字典中的属性时,赋值直接跳过此字段,执行下一字段的赋值

2、如果字典中的关键字是系统保留字,那么在声明属性时会出现与字典关键字不对应,在此方法中就可以完成判断,对应赋值

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

    //内部什么都不做,异常处理,解决赋值个数不匹配的问题

}

原文地址:https://www.cnblogs.com/chongyu/p/5192437.html