iOS文档补完计划--UITableViewCell

目录

  • UITableViewCell
  • 初始化
  • 重用Cell
    • reuseIdentifier
    • prepareForReuse
  • 预定义内容
  • 固有视图
    • contentView
    • backgroundView
  • 附属视图
    • accessoryType
    • accessoryView
  • 选择和高亮
    • selecte
    • selectionStyle
    • highlighted
  • 编辑
    • editing
    • editingStyle
    • showingConfirmation
    • showsReorderControl
  • 状态切换
    • TransitionToState:
  • 内容缩进
    • indentation
    • separatorInset

UITableViewCell

包含属性和方法用于设置和管理Cell的内容和背景(包括文本、图片、和自定义视图)、管理Cell选择和高亮状态、辅助视图、以及编辑内容。

在创建Cell时,您可以自己定制它们或使用几个预定义样式之一。

  1. 使用预定义的样式:
    单元格提供标签和图像子视图,它们的位置和样式是固定的。初始化它使用initWithStyle:reuseIdentifier:方法或配置在Xcode中细胞与风格。要设置单元格的文本和图像,请使用textLabeldetailTextLabelimageView属性。

  2. 定制自己的Cell:
    可以向单元格的contentView属性添加子视图。在添加子视图时,您负责定位这些视图并自己设置它们的内容。

对Cell内容进行配置推荐在tableView:willDisplayCell:forRowAtIndexPath:中进行。


初始化

使用样式和重用标识符初始化

- (instancetype)initWithStyle:(UITableViewCellStyle)style 
              reuseIdentifier:(NSString *)reuseIdentifier;

重用Cell

  • reuseIdentifier

重用标识符

@property(nonatomic, readonly, copy) NSString *reuseIdentifier;

一旦创建了Cell则不可更改

  • - prepareForReuse

即将被重用时将被调用

- (void)prepareForReuse;

一旦Cell即将被dequeueReusableCellWithIdentifier:返回时将被调用。
apple推荐我们在此处进行一些与内容无关的复位操作、比如alpha、编辑和选择状态。
而内容的清空与覆盖则交给tableView:cellForRowAtIndexPath以提升性能。


预定义内容

所有cell都可以使用的三个基本组件

@property (nonatomic, readonly, strong, nullable) UIImageView *imageView NS_AVAILABLE_IOS(3_0);   // 图片

@property (nonatomic, readonly, strong, nullable) UILabel *textLabel NS_AVAILABLE_IOS(3_0);   // 内容
@property (nonatomic, readonly, strong, nullable) UILabel *detailTextLabel NS_AVAILABLE_IOS(3_0);//标题

需要注意的是、这三个属性默认都是nil。并且只有在被使用时才创建该控件并添加在contentView。(这个特性和UIButton一样、能提升很多性能、在我们自己设计控件时很值得借鉴)


固有视图

  • contentView

内容视图

@property(nonatomic, readonly, strong) UIView *contentView;

自定义控件必须要添加在contentView上:

  1. 触发高度的自适应。
  2. 编辑tableView时内容的偏移效果
  • backgroundView

背景视图

@property(nonatomic, strong) UIView *backgroundView;//默认时
@property(nonatomic, strong) UIView *selectedBackgroundView;//单选时
@property(nonatomic, strong) UIView *multipleSelectionBackgroundView;//多选时

该视图处于contentView下方


附属视图

附属视图位于cell右侧

你可以参阅《iOS文档补完计划--UITableViewDataSource&&UITableViewDelegate》中关于扩展按钮的解释

  • accessoryType

默认状态的附属视图类型

@property(nonatomic) UITableViewCellAccessoryType accessoryType;

默认是UITableViewCellAccessoryNone

  • accessoryView

默认状态的附属视图

@property(nonatomic, strong) UIView *accessoryView;

如果该值不为nil、将会忽略accessoryType属性的设置。


选择和高亮

  • selecte

选择状态

@property(nonatomic, getter=isSelected) BOOL selected;
- (void)setSelected:(BOOL)selected 
           animated:(BOOL)animated;

这个值与你是否显示的展示选择状态(selectionStyle)无关。
可以影响标签、图像和背景的外观。

  • selectionStyle

选择状态的样式

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle;

默认UITableViewCellSelectionStyleBlue、通过设置为UITableViewCellSelectionStyleNone可以关闭选择效果。

  • highlighted

高亮状态

@property(nonatomic, getter=isHighlighted) BOOL highlighted;
- (void)setHighlighted:(BOOL)highlighted 
              animated:(BOOL)animated;

要使高亮显示正常工作,您必须使用textLabel和detailTextLabel属性获取单元格的标签,并设置每个标签的highlightedTextColor属性;对于图像,使用imageView属性获取单元格的图像,并设置UIImageView对象的highlightedImage属性。


编辑

  • editing

编辑状态

@property(nonatomic, getter=isEditing) BOOL editing;
- (void)setEditing:(BOOL)editing 
          animated:(BOOL)animated;
  • editingStyle

编辑状态样式

@property(nonatomic, readonly) UITableViewCellEditingStyle editingStyle;

默认值UITableViewCellEditingStyleNone(不可编辑)
tableView:editingStyleForRowAtIndexPath:返回特定的值

  • showingConfirmation

当前是否正在显示删除按钮

@property(nonatomic, readonly) BOOL showingDeleteConfirmation;
  • showsReorderControl

设置是否显示cell自带的自动排序控件

@property(nonatomic) BOOL showsReorderControl;

状态切换

  • TransitionToState:

单元格状态之间转换之前被调用(例如从正常状态(默认状态)到编辑模式)

- (void)willTransitionToState:(UITableViewCellStateMask)state;
- (void)didTransitionToState:(UITableViewCellStateMask)state

自定义单元格可以设置和定位新状态下出现的任何新视图。然后,cell接收到layoutSubviews消息(UIView),在该消息中,它可以将这些新视图放置在新状态的最终位置。当重写此方法时,子类必须始终调用super。


内容缩进

  • indentation

内容缩进等级和缩进宽度

@property(nonatomic) NSInteger indentationLevel;//内容缩进等级 。默认0
@property(nonatomic) CGFloat indentationWidth;//内容缩进宽度 。默认10
@property(nonatomic) BOOL shouldIndentWhileEditing;//编辑模式下是否缩进

《iOS自定义UITableViewCell不支持缩进的解决》中写道只作用于预定义的cell的textLable

  • separatorInset

内容的内嵌值

@property(nonatomic) UIEdgeInsets separatorInset;

只采用左右、上下的值会被忽略。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章