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