目錄
- UITableViewCell
- 初始化
-
重用Cell
- reuseIdentifier
- prepareForReuse
- 預定義內容
-
固有視圖
- contentView
- backgroundView
- 附屬視圖
- accessoryType
- accessoryView
- 選擇和高亮
- selecte
- selectionStyle
- highlighted
- 編輯
- editing
- editingStyle
- showingConfirmation
- showsReorderControl
- 狀態切換
- TransitionToState:
- 內容縮進
- indentation
- separatorInset
UITableViewCell
包含屬性和方法用於設置和管理Cell的內容和背景(包括文本、圖片、和自定義視圖)、管理Cell選擇和高亮狀態、輔助視圖、以及編輯內容。
在創建Cell時,您可以自己定製它們或使用幾個預定義樣式
之一。
使用預定義的樣式:
單元格提供標籤和圖像子視圖,它們的位置和樣式是固定的。初始化它使用initWithStyle:reuseIdentifier:方法或配置在Xcode中細胞與風格。要設置單元格的文本和圖像,請使用textLabel
、detailTextLabel
和imageView
屬性。定製自己的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
上:
- 觸發高度的自適應。
- 編輯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;
只採用左右、上下的值會被忽略。