UITableView
来源:互联网 发布:android 91 桌面 源码 编辑:程序博客网 时间:2024/06/03 19:09
UITableView中有很多行,但是每行有且只有一列,因为继承自UIScrollView,所以会滚动
tableView的常见属性:
*rowHeight 可以统一设置行高
*separatorColor 分割线的颜色
*separatorStyle 分割线的样式
*tableHeaderView 一般可以放广告
*tableFooterView 一般可以放加载更多
设置UiTAbleView的行不允许被选中
self.tableView.allowsSelection = NO;
UITableView显示数据步骤:
1>设置数据源对象
2>数据源对象必须遵守UITableView的数据源协议UITableViewDataSource
3>在数据源对象中,必须实现UITableViewDataSource协议中的某些特定方法(通常有三个)。这些方法的作用就是告诉UITableView该如何显示数据。
三个数据源方法
1、告诉UITableView有几组数据(这个可以不实现,不实现默认就是分一组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2、告诉UITableView每组显示几条(行)数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3、告诉UITableView每一组的每一行显示什么单元格内容
NSIndexPath中封装了两个对象:
indexPath.section :表示当前是第几组
indexPath.row :表示当前是第几行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1、获取模型数据
// 2、创建单元格
// 3、为单元格指定数据
// 4、返回单元格
}
其他常用的 数据源方法:
组标题与组描述
每一组的组标题显示什么
- (NSString *)tableView: (UITableView *)tableView titleForHeaderInSection:(NSInteger) section;
每一组的“组尾”(组描述)
- (NSString *)tableView: (UITableView *)tableView titleForFooterInSection:(NSInteger )section
右侧索引栏
// 右侧索引栏(数据源方法)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.groups valueForKeyPath:@"title"];
}
valueForKeyPath这是KVC的一个方法
解释:
// 因为array本身没有name属性,所以下面这个方法调用,会返回array这个数组中的每个元素对象的name属性的值,会把这些name放到一个新的NSArray中返回
NSArray *arr = [array valueForKeyPath:@"name"];
代理:
监听行被选中的代理
- (void)tableView;(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
设置表头控件:
// 设置表头
self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
设置表尾控件:
// 设置表尾
self.tableView.tableFooterView = [[UISwitchalloc]init];
// 统一设置UITableView的所有行的行高
// 如果每行的行高是一样的,那么通过rowHeight统一设置行高效率更高
self.tableView.rowHeight = 60;
// 对于每行的行高不一样的情况, 无法通过tableView.rowHeight来实现
// 此时,只能通过一个代理方法来实现。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 右侧索引栏(数据源方法)
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.groups valueForKeyPath:@"title"];
}
返回表格的编辑状态
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
<span style="font-size:18px;">- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ // 如果编辑风格为删除 if (editingStyle == UITableViewCellEditingStyleDelete) { [self.contacts removeObjectAtIndex:indexPath.row]; // 刷新表格 NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; [self.tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade]; // 同步数据 [NSKeyedArchiver archiveRootObject:self.contacts toFile:self.contactPath]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { CZContact *model = [[CZContact alloc] init]; model.name = @"中国移动"; model.tel = @"10086"; [self.contacts insertObject:model atIndex:indexPath.row + 1]; // 刷新表格 NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade]; // 同步数据 [NSKeyedArchiver archiveRootObject:self.contacts toFile:self.contactPath]; }}</span>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cell的常见属性:
*imageView
*textLabel
*detailTextLabel
*accessoryType 右边的附加控件(系统默认的)
*accessoryView 右边的附加控件(自定义的)
*backgroundCollor 背景色
*backGroundView 背景控件
*selectedBackgroundView 选中时的背景控件
//局部刷新,刷新指定的行(重新载入行的数据)
// 创建一个行对象
NSIndexPath *idxPath = [NSIndexPathindexPathForRow:alertView.tag inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationLeft];
// 刷新数据(插入指定行)
NSIndexPath *index = [NSIndexPathindexPathForRow:self.contacts.count - 1 inSection:0];
[self.tableViewinsertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
// 刷新表格(删除指定行)
NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
[self.tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
刷新tableView(重新刷新数据的意思就是重新调用UITableView的数据源对象中的那些数"据源方法")
reloadData表示刷新整个tableView
[self.tableView reloadData]; // 重新刷新table view
* 什么是静态单元格?什么是动态单元格?
1> 静态单元格不会随着数据的改变而改变,当在storyboard中设计的时候是什么样子,最后运行效果就是什么样子,并且不会随着数据的变化而变化。如果要想改变静态单元格内容,必须重新修改代码。
2> 动态单元格在设计的时候只是将单元格的"框架(壳子)"设计好了,设置好了位置、大小、背景色等基本信息,里面的具体数据内容, 需要在程序运行时,通过动态获取数据再显示到单元格中。优点:只要修改了数据模型, 那么对应的单元格内容就发生变化了。
* 静态单元格使用建议:
1> 先保留1个Section, 1个Cell。
2> 设置好这个Cell以后,在设置section的个数以及每个section中行的个数。
* 静态单元格设置大致步骤:
首先先拖一个UITableViewController控制器,并设置为启动控制器
1> 选中TableView设置Content为static cell(静态单元格)
2> 删除静态单元格,只保留一个。
3> 选中TableView设置style为grouped
4> 选中单元格,设置设置style为basic、设置Accessory为Disclosure Indicator。
5> 双击"Title"设置文字.
6> 选中单元格设置Image属性为: found_icons_qzone
6.1> 选中 UITableView设置 style为 Grouped
7> 选中tableView设置Section为 3。
8> 选中第二个Section设置Rows为 2
9> 选中第三个Section设置Rows为 3
**注意:当设置了数据源对象,并且实现了数据源方法的时候,即便有静态单元格也优先调用数据源方法来生成数据。
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView
- UITableview
- UITableView
- UITableView
- UITableView
- UITableView
- UITableView,
- UITableView
- UITableView
- UITableView
- UITableView
- 将DataGridView转换为DataTable
- R语言-数字、字符与向量
- java反射机制及简单工厂模式
- HDU-5119-Happy Matt Friends
- Codeforces Round #323 C GCD Table
- UITableView
- Cocos2d-x 开发环境搭建
- 学生选课系统中关于窗口关闭又打开不刷新
- IOS 存储和读取用户首项数据
- 1213:How Many Tables
- C#窗体操作SQL Server存储大文件方法分享
- 数据结构应用案例——栈结构用于8皇后问题的回溯求解
- LintCode -- 最小路径和
- 移除xml中的view对象引出的错误