初识UITableView

一.是什么

a.在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView ,UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳

b.两种格式:UITableViewStylePlain  和  UITableViewStyleGrouped

c.UITableView需要一个数据源(dataSource)来显示数据,凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源


二.展示数据源的方法

1.调用数据源的下面方法得知一共有多少组数据

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;

2. 调用数据源的下面方法得知每一组有多少行数据

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;

3. 调用数据源的下面方法得知每一行显示什么内容

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

ps:头尾标题

/**
 *  第section组显示怎样的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"**";
    } else if (section == 1) {
        return @"**";
    } else {
        ........
    }
}

/**
 *  第section组显示怎样的尾部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return @"**";
    } else if(section == 1) {
        return @"**";
    } else {
        return @"**";
    }
}


三.代理方法

行高一致

self.tableView.rowHeight = 60;

行高不一致

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) return **;
    return ...;
}


四.cell

1 style

2.可以通过UITableViewCelltextLabeldetailTextLabel 和imageView属性访问子控视图

3.重用原理:

当滚动列表时,部分 UITableViewCell 会移出窗口, UITableView 会将窗口外的 UITableViewCell 放入一个对象池中,等待重用。当 UITableView 要求 dataSource 返回 UITableViewCell 时, dataSource 会先查看这个对象池,如果池中有未使用的 UITableViewCell dataSource 会用新的数据配置这个 UITableViewCell ,然后返回给 UITableView ,重新显示到窗口中,从而避免创建新对象
ps:
有时候需要自定义 UITableViewCell( 用一个子类继承 UITableViewCell) ,而且每一行用的不一定是同一种 UITableViewCell ,所以一个 UITableView 可能拥有不同类型的 UITableViewCell ,对象池中也会有很多不同类型的 UITableViewCell ,那么 UITableView 在重用 UITableViewCell 时可能会得到错误类型的 UITableViewCell
bei• 解决方案 UITableViewCell 有个 NSString * reuseIdentifier 属性,可以在初始化 UITableViewCell 的时候传入一个特定的字符串标识来设置 reuseIdentifier( 一般用 UITableViewCell 的类名 ) 。当 UITableView 要求 dataSource 返回 UITableViewCell 时,先通过一个字符串标识到对象池中查找对应类型的 UITableViewCell 对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个 UITableViewCell 对象

4.重用代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.定义一个cell的标识
      static NSString *ID = @"mjcell";
    
    // 2.从缓存池中取出cell
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 3.如果缓存池中没有cell
      if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    // 4.设置cell的属性...
    
      return cell;
}

不重用时
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

5.其他
UITableViewCell 内部有个默认 的子视图 : contentView contentView UITableViewCell 所显示内容的父视图 ,可显示一些 辅助指示视图
辅助指示视图 的作用是显示一个表示动作的图标,可以通过设置 UITableViewCell accessoryType 来显示,默认是 UITableViewCellAccessoryNone( 不显示辅助指示视图 ) ,其他值如下 :
 UITableViewCellAccessoryNone,                   // don't show any accessory view

 UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track    标示:>

 UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks  标示:圈i 和 >

 UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track               标示:对勾
 
 UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0)                标示:圈i
还可以通过 cell accessoryView 属性来自定义辅助指示视图(比如往右边放一个开关)
cell.accessoryView = [[UISwitch alloc] init];

背景:
// 设置背景(背景view不用设置尺寸, backgroundView的优先级 > backgroundColor)
    UIImageView *bgView = [[UIImageView alloc] init];
    bgView.image = [UIImage imageNamed:@"buttondelete"];
   // bgView.backgroundColor = [UIColor redColor];
    cell.backgroundView = bgView;
    
    UIView *selectedbgView = [[UIView alloc] init];
    selectedbgView.backgroundColor = [UIColor greenColor];
    cell.selectedBackgroundView = selectedbgView;





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值