IOS中UITableViewCell使用詳解
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier;
Cell的初始化方法,可以設(shè)置一個(gè)風(fēng)格和標(biāo)識(shí)符,風(fēng)格的枚舉如下:
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // 默認(rèn)風(fēng)格,自帶標(biāo)題和一個(gè)圖片視圖,圖片在左
UITableViewCellStyleValue1, // 只有標(biāo)題和副標(biāo)題 副標(biāo)題在右邊
UITableViewCellStyleValue2, // 只有標(biāo)題和副標(biāo)題,副標(biāo)題在左邊標(biāo)題的下邊
UITableViewCellStyleSubtitle // 自帶圖片視圖和主副標(biāo)題,主副標(biāo)題都在左邊,副標(biāo)題在下
};
@property (nonatomic, readonly, retain) UIImageView *imageView;
圖片視圖,風(fēng)格允許時(shí)才會(huì)創(chuàng)建
@property (nonatomic, readonly, retain) UILabel *textLabel;
標(biāo)題標(biāo)簽
@property (nonatomic, readonly, retain) UILabel *detailTextLabel;
副標(biāo)題標(biāo)簽
@property (nonatomic, readonly, retain) UIView *contentView;
容納視圖,任何cell的子視圖都應(yīng)該添加在這個(gè)上面
@property (nonatomic, retain) UIView *backgroundView;
背景視圖
@property (nonatomic, retain) UIView *selectedBackgroundView;
選中狀態(tài)下的背景視圖
@property (nonatomic, retain) UIView *multipleSelectionBackgroundView;
多選選中時(shí)的背景視圖
@property (nonatomic, readonly, copy) NSString *reuseIdentifier;
cell的標(biāo)識(shí)符
- (void)prepareForReuse;
當(dāng)被重用的cell將要顯示時(shí),會(huì)調(diào)用這個(gè)方法,這個(gè)方法最大的用武之地是當(dāng)你自定義的cell上面有圖片時(shí),如果產(chǎn)生了重用,圖片可能會(huì)錯(cuò)亂(當(dāng)圖片來自異步下載時(shí)及其明顯),這時(shí)我們可以重寫這個(gè)方法把內(nèi)容抹掉。
@property (nonatomic) UITableViewCellSelectionStyle selectionStyle;
cell被選中時(shí)的風(fēng)格,枚舉如下:
typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone,//無
UITableViewCellSelectionStyleBlue,//藍(lán)色
UITableViewCellSelectionStyleGray,//灰色
UITableViewCellSelectionStyleDefault//默認(rèn) 為藍(lán)色
};
@property (nonatomic, getter=isSelected) BOOL selected;
設(shè)置cell是否選中狀態(tài)
@property (nonatomic, getter=isHighlighted) BOOL highlighted;
設(shè)置cell是否高亮狀態(tài)
(void)setSelected:(BOOL)selected animated:(BOOL)animated;
(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated;
與上面的兩個(gè)屬性對(duì)應(yīng)
@property (nonatomic, readonly) UITableViewCellEditingStyle editingStyle;
獲取cell的編輯狀態(tài),枚舉如下
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,//無編輯
UITableViewCellEditingStyleDelete,//刪除編輯
UITableViewCellEditingStyleInsert//插入編輯
};
@property (nonatomic) BOOL showsReorderControl;
設(shè)置是否顯示cell自帶的自動(dòng)排序控件
注意:要讓cell實(shí)現(xiàn)拖動(dòng)排序的功能,除了上面設(shè)置為YES,還需實(shí)現(xiàn)代理中的如下方法:
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
}
@property (nonatomic) BOOL shouldIndentWhileEditing;
設(shè)置編輯狀態(tài)下是否顯示縮進(jìn)
@property (nonatomic) UITableViewCellAccessoryType accessoryType;
設(shè)置附件視圖的風(fēng)格(cell最右側(cè)顯示的視圖) 枚舉如下:
typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
UITableViewCellAccessoryNone, // 沒有視圖
UITableViewCellAccessoryDisclosureIndicator, // cell右側(cè)顯示一個(gè)灰色箭頭
UITableViewCellAccessoryDetailDisclosureButton, // 顯示詳情符號(hào)和灰色箭頭
UITableViewCellAccessoryCheckmark, // cell右側(cè)顯示藍(lán)色對(duì)號(hào)
UITableViewCellAccessoryDetailButton // cell右側(cè)顯示一個(gè)詳情符號(hào)
};
@property (nonatomic, retain) UIView *accessoryView;
附件視圖
@property (nonatomic) UITableViewCellAccessoryType editingAccessoryType;
cell編輯時(shí)的附件視圖風(fēng)格
@property (nonatomic, retain) UIView *editingAccessoryView;
cell編輯時(shí)的附件視圖
@property (nonatomic) NSInteger indentationLevel;
設(shè)置內(nèi)容區(qū)域的縮進(jìn)級(jí)別
@property (nonatomic) CGFloat indentationWidth;
設(shè)置每個(gè)級(jí)別的縮進(jìn)寬度
@property (nonatomic) UIEdgeInsets separatorInset;
設(shè)置分割線的偏移量
@property (nonatomic, getter=isEditing) BOOL editing;
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
設(shè)置是否編輯狀態(tài)
@property(nonatomic, readonly) BOOL showingDeleteConfirmation;
返回是否目前正在顯示刪除按鈕
- (void)willTransitionToState:(UITableViewCellStateMask)state;
cell狀態(tài)將要轉(zhuǎn)換時(shí)調(diào)用的函數(shù),可以在子類中重寫
- (void)didTransitionToState:(UITableViewCellStateMask)state;
cell狀態(tài)已經(jīng)轉(zhuǎn)換時(shí)調(diào)用的函數(shù),可以在子類中重寫,狀態(tài)枚舉如下:
typedef NS_OPTIONS(NSUInteger, UITableViewCellStateMask) {
UITableViewCellStateDefaultMask = 0,//默認(rèn)狀態(tài)
UITableViewCellStateShowingEditControlMask = 1 << 0,//編輯狀態(tài)
UITableViewCellStateShowingDeleteConfirmationMask = 1 << 1//確認(rèn)刪除狀態(tài)
};
注意:下面這些方法已經(jīng)全部在IOS3.0后被廢棄了,雖然還有效果,但是會(huì)被警告
@property (nonatomic, copy) NSString *text;
設(shè)置標(biāo)題
@property (nonatomic, retain) UIFont *font;
設(shè)置字體
@property (nonatomic) NSTextAlignment textAlignment;
設(shè)置對(duì)其模式
@property (nonatomic) NSLineBreakMode lineBreakMode;
設(shè)置斷行模式
@property (nonatomic, retain) UIColor *textColor;
設(shè)置字體顏色
@property (nonatomic, retain) UIColor *selectedTextColor;
設(shè)置選中狀態(tài)下的字體顏色
@property (nonatomic, retain) UIImage *image;
設(shè)置圖片
@property (nonatomic, retain) UIImage *selectedImage;
設(shè)置選中狀態(tài)時(shí)的圖片
@property (nonatomic) BOOL hidesAccessoryWhenEditing;
設(shè)置編輯的時(shí)候是否隱藏附件視圖