iOS设计中最重要的概念之一:重用机制
UITableViewCell的设计使用的就是重用机制,这大大节省了资源的占用。这也是设计中要注意的点,很多情况下tableViewCell不能正常显示,或者是显示有差异,有很大一部分的原因就是重用机制没有运用好。
重用机制:
显示在屏幕上的cell是实实在在创建出来的,同样类型的cell都有相同的标志identifier,当最上面的cell随着滑动消失,下一个即将要被显示出来的cell同时也被创建出来。
再接着向上滑动,隐藏的cell自动进入重用池,但是cell自身之前显示的内容并没有清楚,还依然在cell的身上。
再下一个将要显示的cell,首先依据identifier从重用池寻找相同类型的cell,如果有,就将自身的属性覆盖cell原先的属性,并显示出来。如果没有就创建出来再设置属性并显示。
从网上找的一段复用cell的过程
根据说明,查看UITableView,可以找到:
@property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells;
此属性可以直接用来进行设置和操作。
查看UITableViewCell的API:
// 常用属性和方法
NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableViewCell : UIView <NSCoding, UIGestureRecognizerDelegate>
// 创建方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier NS_AVAILABLE_IOS(3_0) NS_DESIGNATED_INITIALIZER;
// tableviewCell的样式
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
};
// tableviewCell的分割线样式
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched // 在tableviewStyle是grouped下才作用
}
// 使用UITableViewCell最常用的属性
@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);
// 设置cell的背景
@property (nonatomic, strong, nullable) UIView *backgroundView;
@property (nonatomic, strong, nullable) UIView *selectedBackgroundView;
// 自定义cell布局控件的superView
@property (nonatomic, readonly, strong) UIView *contentView;
// 标志
@property (nonatomic, readonly, copy, nullable) NSString *reuseIdentifier;
// 选中的样式
@property (nonatomic) UITableViewCellSelectionStyle selectionStyle;
// tableviewCell选中的样式
typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
};
// 选中属性和方法
@property (nonatomic, getter=isSelected) BOOL selected;
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
// 高亮属性和方法
@property (nonatomic, getter=isHighlighted) BOOL highlighted;
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated;
// 编辑样式
@property (nonatomic, readonly) UITableViewCellEditingStyle editingStyle;
// tableViewCell编辑样式
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
};
// 辅助样式
@property (nonatomic) UITableViewCellAccessoryType accessoryType;
// tableViewCell辅助属性
typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
UITableViewCellAccessoryNone,
UITableViewCellAccessoryDisclosureIndicator,
UITableViewCellAccessoryDetailDisclosureButton __TVOS_PROHIBITED,
UITableViewCellAccessoryCheckmark,
UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED
};
// 自定义辅助view
@property (nonatomic, strong, nullable) UIView *accessoryView;
// 编辑时的辅助样式
@property (nonatomic) UITableViewCellAccessoryType editingAccessoryType;
// 自定义编辑辅助样式
@property (nonatomic, strong, nullable) UIView *editingAccessoryView;
// 设置cell的边界
@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;
// 设置编辑
@property (nonatomic, getter=isEditing) BOOL editing;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
@end