UI初级第七课  表视图的使用——iOS学习连载22

来源:互联网 发布:网络用语盘点 编辑:程序博客网 时间:2024/06/06 12:58
1.创建一个表视图
   
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];
   
   //通过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;
}
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;
}
12.删除第一个元素
[self.dataremoveObjectAtIndex:0];
必须刷新表视图才能够删除
[self.tableViewreloadData];
13.//把第2个单元格的内容改为“hello
   //构建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;
}
18.让当前视图控制器中的第一个滑动视图默认往下偏移64个像素
self.automaticallyAdjustsScrollViewInsets =NO;
19.排序----compare:是系统的排序方法
allKeys = [allKeyssortedArrayUsingSelector:@selector(compare:)];
20. //设置组索引的颜色
    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];
   
   return cell; 
}
 
0 0