UIKit框架-11.UITableViewCell概述

1.UITableViewCell概述

  • tableView之所以能够显示列表数据,是因为tableView内部有一个子控件UITableViewCel能够显示每一行的内容
  • UITableViewCell中有很多子控件,能够显示多种样式的表格内容

2.UITableViewCell基本使用

  • 常用初始化方法
//UITableViewCellStyle有三种样式
/*
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault, 默认样式,可以显示图片和标题
    UITableViewCellStyleValue1,     样式1:detail标签以蓝色文字紧靠标题右边
    UITableViewCellStyleValue2, 样式2:    标题文字在左边,正文文字在最右边,和cell右边对齐
    UITableViewCellStyleSubtitle 子标题样式,子标题在标题下边,整体位于cell的左部
}
*/
// reuseIdentifier:为重用标示,为了防止重复创建cell,tableview提供了缓存池的概念,我们创建cell,会先到缓存池中找是否有标有重用标示的cell可用,如果没有才会重新创建一个新的cell
// 定义一个局部静态变量
 static NSString *identifier = @"cell";
 // 取缓存池中重用cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 判断缓存池中是否取到可用cell,没有就重新创建
if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
// 说明,在一行cell滚出视图范围后,系统会自动将它放入tableView的缓存池中,并保留重用标示,所以为了找到同样样式的cell,必须给cell设定重用标示,不然我们无法判断找到的cell是否满足需求
  • 设置cell内部子控件内容
// cell内部的子控件都是放置在contentView属性中的,所以在数据源方法返回每一行cell的时候我们可以设置cell的子控件内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    1.创建cell
    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //    2.设置cell的数据
    // 2.1设置图片
    cell.imageView.image = [UIImage imageNamed:@"tupian"];
    // 2.2设置标题
    cell.textLabel.text = [NSString stringWithFormat:@"我是数据%zd组",indexPath.section];
    // 2.3设置子标题
    cell.detailTextLabel.text = [NSString stringWithFormat:@"我是数据%zd行",indexPath.row];
    //    3.返回cell
    return cell;
}
  • contentView

    • cell的子控件的容器视图
    • contentView中一般包含有一个imageView、一个textLabel、一个detailTextLabel
  • cell右边辅助样式

/*
typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
    UITableViewCellAccessoryNone,   无样式
    UITableViewCellAccessoryDisclosureIndicator, 箭头样式   
    UITableViewCellAccessoryDetailDisclosureButton, 按钮+箭头样式
    UITableViewCellAccessoryCheckmark,   ✅样式
    UITableViewCellAccessoryDetailButton 详细按钮样式
};
*/
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 若是系统自带样式满足不了需求,我们也可以使用自定义视图
设置cell.accessoryView属性
  • cell背景颜色以及选中颜色
// 设置背景视图和背景颜色
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor redColor];
    cell.backgroundView = bg;
    cell.backgroundColor = [UIColor yellowColor];
// backgroundView它的优先级比backgroundColor高,并且这俩是并存    

// 设置选中时的背景颜色和背景视图    
//    cell.selectedBackgroundView = bg;
// 系统给我们提供了一些样式
/*
typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
    UITableViewCellSelectionStyleNone, 无颜色
    UITableViewCellSelectionStyleBlue, 选中变蓝色(在iOS7之后和Gray效果相同)
    UITableViewCellSelectionStyleGray, 选中变灰色
    UITableViewCellSelectionStyleDefault 默认样式灰色
};
*/
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
// selectedBackgroundView设置选中背景优先级比selectionStyle高
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值