UI初级第七课 表视图的使用——iOS学习连载22
来源:互联网 发布:网络用语盘点 编辑:程序博客网 时间:2024/06/06 12:58
1.创建一个表视图
UITableView *tableView = [[UITableViewalloc]initWithFrame:self.view.bounds];
UITableView *tableView = [[UITableViewalloc]initWithFrame:self.view.bounds];
2.以代理对方式设置数据源,显示什么数据由代理对象来确定
tableView.dataSource= self;
tableView.backgroundColor= [UIColorgreenColor];
[self.viewaddSubview:tableView];
3.获取数据源
array = [UIFontfamilyNames];
4.#pragma mark -UITableViewDataSource
//当前显示的数据有多少个
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
//返回每一条数据对应的单元格对象
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//初始化一个单元格
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];
//当前显示的数据有多少个
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
//返回每一条数据对应的单元格对象
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//初始化一个单元格
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];
//通过indexPath拿到构建单元格的行的下标
NSLog(@"section:--%ld, row:--%ld", indexPath.section, indexPath.row);
NSString *fonName = [arrayobjectAtIndex:indexPath.row];
cell.textLabel.text= fonName;
cell.textLabel.font= [UIFontfontWithName:fonNamesize:20];
return cell;
NSString *fonName = [arrayobjectAtIndex:indexPath.row];
cell.textLabel.text= fonName;
cell.textLabel.font= [UIFontfontWithName:fonNamesize:20];
return cell;
}
5.表视图的样式:UITableViewStyleGrouped:分组样式
UITableViewStylePlain:平铺样式(默认)
6.设置tableView的内容偏移量
tableView.contentInset =UIEdgeInsetsMake(20,0, 0,0);
7.获取文件路径方法一:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"plist"];
方法二:
//拿到bundle的源路径
NSString *resourcePath = [[NSBundlemainBundle] resourcePath];
//文件路径
NSString *filePath = [resourcePathstringByAppendingPathComponent:@"font.plist"];
8.根据一个文件路径返回一个数组
self.data = [NSArrayarrayWithContentsOfFile:filePath];
9.设置表视图分割线风格:
//UITableViewCellSeparatorStyleNone:没有分割线
_tableView.separatorStyle= UITableViewCellSeparatorStyleNone;
10.设置表视图分割线颜色,默认标准灰色
_tableView.separatorColor = [UIColorredColor];
11. //设置单元格的行高--默认44
_tableView.rowHeight =100;
以上方法只能更改所有单元格的行高均为100,如果需要动态地确定行高需要在代理方法中实现
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.row== 0) {
return 100;
}elseif (indexPath.row== 1) {
return 200;
}
return 44;
{
if (indexPath.row== 0) {
return 100;
}elseif (indexPath.row== 1) {
return 200;
}
return 44;
}
12.删除第一个元素
[self.dataremoveObjectAtIndex:0];
必须刷新表视图才能够删除
[self.tableViewreloadData];
13.//把第2个单元格的内容改为“hello”
//构建IndexPath
NSIndexPath *indexPath = [NSIndexPathindexPathForRow:2inSection:0];
//取到单元格
UITableViewCell *cell = [self.tableViewcellForRowAtIndexPath:indexPath];
NSIndexPath *indexPath = [NSIndexPathindexPathForRow:2inSection:0];
//取到单元格
UITableViewCell *cell = [self.tableViewcellForRowAtIndexPath:indexPath];
cell.textLabel.text =@"hello";
注意:不能刷新表视图,因为刷新表视图会重新执行数据源方法和代理方法,将会重写第1个单元格的内容,即仍为原来单元格的内容
14.输出在屏幕上显示的单元格的内容
NSArray*cells = [self.tableViewvisibleCells];
for(UITableViewCell*cell in cells) {
NSLog(@"%@", cell.textLabel.text);
}
15.输出在屏幕上显示的单元格的下标
NSArray*indexPaths = [self.tableViewindexPathsForVisibleRows];
for(NSIndexPath*indexPath in indexPaths) {
NSLog(@"%ld", indexPath.row);
}
16.滑动到指定的位置,可以配置动画
NSIndexPath*indexPath = [NSIndexPathindexPathForRow:10inSection:0];
[self.tableViewscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTopanimated:true];
17.返回自定义的组的头视图(一定要实现返回组头视图高度的协议方法)
//组头视图高度的协议方法
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
{
return 50;
}
18.让当前视图控制器中的第一个滑动视图默认往下偏移64个像素
self.automaticallyAdjustsScrollViewInsets =NO;
19.排序----compare:是系统的排序方法
allKeys = [allKeyssortedArrayUsingSelector:@selector(compare:)];
20. //设置组索引的颜色
tableView.sectionIndexColor= [UIColorredColor];
//设置组索引的背景颜色
tableView.sectionIndexBackgroundColor= [UIColorgreenColor];
//设置组索引选中后的颜色
tableView.sectionIndexColor= [UIColorredColor];
//设置组索引的背景颜色
tableView.sectionIndexBackgroundColor= [UIColorgreenColor];
//设置组索引选中后的颜色
tableView.sectionIndexTrackingBackgroundColor = [UIColorblueColor];
21.复用单元格
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//创建一个identifier
NSString *identifier =@"Cell";
//到复用池中取一个复用单元格
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
//如果复用池没有闲置的单元格,则创建
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier]autorelease];
}
cell.textLabel.text= self.data[indexPath.row];
NSString *identifier =@"Cell";
//到复用池中取一个复用单元格
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
//如果复用池没有闲置的单元格,则创建
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier]autorelease];
}
cell.textLabel.text= self.data[indexPath.row];
return cell;
}
0 0
- UI初级第七课 表视图的使用——iOS学习连载22
- UI初级第四课 视图控制器——iOS学习连载18
- UI初级第十课 滑动视图基本概念和用法——iOS学习连载25
- UI初级第二课 窗口与视图基本概念和用法——iOS学习连载16
- UI初级第三课 常用UI控件——iOS学习连载17
- IOS UI初级-表视图的使用
- UI初级第一课 iPhone开发入门——iOS学习连载15
- UI初级第五课 导航控制器——iOS学习连载20
- UI初级第六课 Tab 控制器(标签控制器)——iOS学习连载21
- UI初级第八课 定制单元格——iOS学习连载23
- UI初级第九课 手势与触摸——iOS学习连载24
- UI初级连载九----------UITableView的使用
- 【UI初级--连载八】-------------表视图(UITableView)
- UI初级连载12-------------滑动视图
- IOS UI初级-表视图单元格定制
- UI高级第一课 block语法——iOS学习连载29
- UI高级第二课 多线程和GCD——iOS学习连载30
- UI高级第三课 音频视频——iOS学习连载31
- ZOJ3876 May Day Holiday
- VC中基于Windows的精确定时
- javamail发送邮件
- 06==函数(头文件,声明,封装)
- c#中整型转为字符串型
- UI初级第七课 表视图的使用——iOS学习连载22
- 求余
- java初体验和基本语法问题记录
- Spring MVC form handling example
- Console.Read();
- Rust之语法<1>变量绑定
- 第一篇博客
- 移动APP整体策划-001-移动App项目研发流程及版本规划
- UI初级第八课 定制单元格——iOS学习连载23