IOS UITableViewCell使用詳解

IOS中UITableViewCell使用詳解

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

Cell的初始化方法,能夠設置一個風格和標識符,風格的枚舉以下:
ide

?函數

1
2
3
4
5
6
typedef  NS_ENUM(NSInteger, UITableViewCellStyle) {
     UITableViewCellStyleDefault,    // 默認風格,自帶標題和一個圖片視圖,圖片在左
     UITableViewCellStyleValue1,     // 只有標題和副標題 副標題在右邊
     UITableViewCellStyleValue2,     // 只有標題和副標題,副標題在左邊標題的下邊
     UITableViewCellStyleSubtitle    // 自帶圖片視圖和主副標題,主副標題都在左邊,副標題在下
};


@property (nonatomicreadonlyretainUIImageView *imageView;字體

圖片視圖,風格容許時纔會建立
atom

@property (nonatomicreadonlyretainUILabel     *textLabel;spa

標題標籤
.net

@property (nonatomicreadonlyretainUILabel     *detailTextLabel;代理

副標題標籤
code


@property (nonatomicreadonlyretainUIView      *contentView;orm

容納視圖,任何cell的子視圖都應該添加在這個上面


@property (nonatomicretainUIView                *backgroundView;

背景視圖


@property (nonatomicretainUIView                *selectedBackgroundView;

選中狀態下的背景視圖


@property (nonatomicretainUIView              *multipleSelectionBackgroundView;

多選選中時的背景視圖


@property (nonatomicreadonlycopyNSString      *reuseIdentifier;

cell的標識符


- (void)prepareForReuse; 

當被重用的cell將要顯示時,會調用這個方法,這個方法最大的用武之地是當你自定義的cell上面有圖片時,若是產生了重用,圖片可能會錯亂(當圖片來自異步下載時及其明顯),這時咱們能夠重寫這個方法把內容抹掉。


@property (nonatomicUITableViewCellSelectionStyle   selectionStyle;  

cell被選中時的風格,枚舉以下:

?

1
2
3
4
5
6
typedef  NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
     UITableViewCellSelectionStyleNone, //無
     UITableViewCellSelectionStyleBlue, //藍色
     UITableViewCellSelectionStyleGray, //灰色
     UITableViewCellSelectionStyleDefault //默認 爲藍色
};

@property (nonatomicgetter=isSelected) BOOL         selected;  

設置cell是否選中狀態


@property (nonatomicgetter=isHighlighted) BOOL      highlighted;   

設置cell是否高亮狀態


- (void)setSelected:(BOOL)selected animated:(BOOL)animated;  

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated; 

與上面的兩個屬性對應


@property (nonatomicreadonlyUITableViewCellEditingStyle editingStyle; 

獲取cell的編輯狀態,枚舉以下

?

1
2
3
4
5
typedef  NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
     UITableViewCellEditingStyleNone, //無編輯
     UITableViewCellEditingStyleDelete, //刪除編輯
     UITableViewCellEditingStyleInsert //插入編輯
};


@property (nonatomicBOOL                            showsReorderControl; 

設置是否顯示cell自帶的自動排序控件

注意:要讓cell實現拖動排序的功能,除了上面設置爲YES,還需實現代理中的以下方法:

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    

}


@property (nonatomicBOOL                            shouldIndentWhileEditing;

設置編輯狀態下是否顯示縮進

@property (nonatomicUITableViewCellAccessoryType    accessoryType; 

設置附件視圖的風格(cell最右側顯示的視圖) 枚舉以下:

?

1
2
3
4
5
6
7
typedef  NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
     UITableViewCellAccessoryNone,                    // 沒有視圖
     UITableViewCellAccessoryDisclosureIndicator,     // cell右側顯示一個灰色箭頭
     UITableViewCellAccessoryDetailDisclosureButton,  // 顯示詳情符號和灰色箭頭
     UITableViewCellAccessoryCheckmark,               // cell右側顯示藍色對號
     UITableViewCellAccessoryDetailButton   // cell右側顯示一個詳情符號
};

@property (nonatomicretainUIView                 *accessoryView;  

附件視圖


@property (nonatomicUITableViewCellAccessoryType    editingAccessoryType; 

cell編輯時的附件視圖風格


@property (nonatomicretainUIView                 *editingAccessoryView;  

cell編輯時的附件視圖


@property (nonatomicNSInteger                       indentationLevel; 

設置內容區域的縮進級別

@property (nonatomicCGFloat                         indentationWidth; 

設置每一個級別的縮進寬度

@property (nonatomicUIEdgeInsets                    separatorInset;

設置分割線的偏移量


@property (nonatomicgetter=isEditing) BOOL          editing; 

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

設置是否編輯狀態


@property(nonatomicreadonlyBOOL                   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 (nonatomiccopy)   NSString *text;

設置標題

@property (nonatomicretainUIFont   *font;

設置字體

@property (nonatomicNSTextAlignment   textAlignment;

設置對其模式

@property (nonatomicNSLineBreakMode   lineBreakMode;

設置斷行模式

@property (nonatomicretainUIColor  *textColor;

設置字體顏色

@property (nonatomicretainUIColor  *selectedTextColor;

設置選中狀態下的字體顏色

@property (nonatomicretainUIImage  *image;

設置圖片

@property (nonatomicretainUIImage  *selectedImage;

設置選中狀態時的圖片

@property (nonatomicBOOL              hidesAccessoryWhenEditing;

設置編輯的時候是否隱藏附件視圖

相關文章
相關標籤/搜索