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

1.创建一个表视图
   
UITableView *tableView = [[ UITableView alloc ] initWithFrame : self . view . bounds ];
2.以代理对方式设置数据源,显示什么数据由代理对象来确定
    tableView. dataSource = self ;
    tableView. backgroundColor = [ UIColor greenColor ];
    [self.view addSubview:tableView];
3.获取数据源
    array = [UIFont familyNames];
4. #pragma mark -UITableViewDataSource
// 当前显示的数据有多少个
- (
NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section
{
   
return array . count ;
}

// 返回每一条数据对应的单元格对象
- (
UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
   
// 初始化一个单元格
   
UITableViewCell *cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier : nil ];
   
    // 通过 indexPath 拿到构建单元格的行的下标
    NSLog ( @"section:--%ld, row:--%ld" , indexPath. section , indexPath. row );
   
   
NSString *fonName = [ array objectAtIndex :indexPath. row ];
   
    cell.
textLabel . text = fonName;
    cell.
textLabel . font = [ UIFont fontWithName :fonName size : 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 = [[NSBundle mainBundle] resourcePath];
//文件路径
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"font.plist"];
8. 根据一个文件路径返回一个数组
self.data = [NSArray arrayWithContentsOfFile:filePath];
9. 设置表视图分割线风格 :
//UITableViewCellSeparatorStyleNone:没有分割线
  _tableView . separatorStyle = UITableViewCellSeparatorStyleNone ;
10. 设置表视图分割线颜色,默认标准灰色
_tableView.separatorColor = [UIColor redColor];
11.   // 设置单元格的行高 -- 默认 44   
    _tableView.rowHeight = 100;
以上方法只能更改所有单元格的行高均为100,如果需要动态地确定行高需要在代理方法中实现
- ( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath
{
   
if (indexPath. row == 0 ) {
       
return 100 ;
    }
else if (indexPath. row == 1 ) {
       
return 200 ;
    }
   
return 44 ;
}
12. 删除第一个元素
[self.data removeObjectAtIndex:0];
必须刷新表视图才能够删除
[self.tableView reloadData];
13. // 把第 2 个单元格的内容改为 “hello
    // 构建 IndexPath
   
NSIndexPath *indexPath = [ NSIndexPath indexPathForRow : 2 inSection : 0 ];
   
// 取到单元格
   
UITableViewCell *cell = [ self . tableView cellForRowAtIndexPath :indexPath];
    cell.textLabel.text = @"hello";
注意:不能刷新表视图,因为刷新表视图会重新执行数据源方法和代理方法, 将会重写第1个单元格的内容,即仍为原来单元格的内容
14. 输出在屏幕上显示的单元格的内容   
NSArray *cells = [ self . tableView visibleCells ];
for ( UITableViewCell *cell in cells) {
        NSLog ( @"%@" , cell. textLabel . text );
}
15. 输出在屏幕上显示的单元格的下标
NSArray *indexPaths = [ self . tableView indexPathsForVisibleRows ];
for ( NSIndexPath *indexPath in indexPaths) {
        NSLog ( @"%ld" , indexPath. row );
}
16. 滑动到指定的位置,可以配置动画
    NSIndexPath *indexPath = [ NSIndexPath indexPathForRow : 10 inSection : 0 ];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:true];
17. 返回自定义的组的头视图 ( 一定要实现返回组头视图高度的协议方法 )
//组头视图高度的协议方法
- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section
{
   
return 50 ;
}
18. 让当前视图控制器中的第一个滑动视图默认往下偏移 64 个像素
self.automaticallyAdjustsScrollViewInsets = NO;
19. 排序 ----compare: 是系统的排序方法
allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
20.   // 设置组索引的颜色
    tableView.
sectionIndexColor = [ UIColor redColor ];
   
// 设置组索引的背景颜色
    tableView.
sectionIndexBackgroundColor = [ UIColor greenColor ];
   
// 设置组索引选中后的颜色
    tableView.sectionIndexTrackingBackgroundColor = [UIColor blueColor];
21. 复用单元格
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
    // 创建一个 identifier
   
NSString *identifier = @"Cell" ;
   
   
// 到复用池中取一个复用单元格
   
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :identifier];
   
   
if (cell == nil ) {
       
// 如果复用池没有闲置的单元格,则创建
        cell = [[[
UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :identifier] autorelease ];
    }
   
    cell.
textLabel . text = self . data [indexPath. row ];
   
    return cell;  
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值