ios--UITableViewCell 使用方法詳解

UITableViewCell:

1.使用系統自定義的各種UITableViewCell的樣式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indentifier = @"cell";
    MyTableCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        /*
         
         typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
         UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
         UITableViewCellStyleValue1,        // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
         UITableViewCellStyleValue2,        // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
         UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
         };
         */

        cell = [[[MyTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]autorelease];
    }
    cell.textLabel.text = [_data objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = @"detail";
    cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
    return cell;
}

使用UITableViewCellStyleDefault的效果:


使用UITableViewCellStyleValue1的效果:



使用UITableViewCellStyleValue2的效果:


在UITableViewCell內默認是有contentview和accessoryView這兩個subview的,contentview中的subview根據不同的cell的style會使用不同的佈局。contentview和其中的默認subview會根據cell的編輯狀態出現的控件自動縮進,自定義cell時可以把自定義控件添加在contentview中,也可以直接添加到cell中。


2.設置UITableViewCell的屬性

      //cell的右邊輔助按鈕的樣式
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    //自定義cell右邊的輔助按鈕
    cell.accessoryView = nil;
    //自定義cell的背景
    cell.backgroundView = nil;
    //設置cell的contentview中的detail的文字內容
    cell.detailTextLabel.text = @"";
    //查看cell當前的編輯模式
    int style = cell.editingStyle;
    //設置當cell進入編輯模式時的輔助按鈕樣式
    cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //自定義cell進入編輯模式後輔助按鈕
    cell.editingAccessoryView = nil;
    //獲取cell的縮進級別
    int level = cell.indentationLevel;
    //獲取cell的縮進寬度
    float width = cell.indentationWidth;
    //設置cell被選中時的背景
    cell.selectedBackgroundView = nil;
    //設置cell的選中狀態樣式
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    //設置cell的contentview中的textlabel文字內容
    cell.textLabel.text = @"";


3.自定義的UITableViewCell重寫父類的方法

//初始化uitableviewcell後,自定義cell添加subview
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

//當cell被選中時,uitableview內部會自動調用該方法,重寫該方法可以在cell被選中時做一些額外的操作
- (void)setSelected:(BOOL)selected animated:(BOOL)animated

//當cell處於高亮狀態時,uitableview內部會自動調用該方法,重寫該方法可以在cell處於高亮時做一些額外操作
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

//重寫layoutsubviews方法,爲了查看當cell改變編輯狀態時,有什麼subview
-(void)layoutSubviews{

    [super layoutSubviews];
    NSArray* subs = self.subviews;
    for (UIView* sub in subs) {
        NSLog(@"view:%@",sub);
    }
}

當進入刪除編輯模式時,cell的subview有一個叫UITableViewCellDeleteConfirmationControl的subview,這代表刪除按鈕。可以修改該view達到修改刪除按鈕的位置,大小等屬性。

當進入移動編輯模式時,cell的subview有一個叫UITableViewCellReorderControl的subview,這個代表移動按鈕。可以修改該view達到修改移動按鈕的位置,大小等屬性。

當進入插入編輯模式時,cell的subview有一個叫UITableViewCellEditControl的subview,這個代表添加按鈕。可以修改該view達到修改添加按鈕的位置,大小等屬性。


//當cell的狀態變爲編輯時,uitableview內部會自動調用該方法,重寫該方法可以改變cell的佈局
-(void)willTransitionToState:(UITableViewCellStateMask)state{
    [super willTransitionToState:state];
}
//當cell的狀態變爲編輯時,uitableview內部會自動調用該方法,重寫該方法可以改變cell的佈局
-(void)didTransitionToState:(UITableViewCellStateMask)state{
    [super didTransitionToState:state];
    /*
     typedef NS_OPTIONS(NSUInteger, UITableViewCellStateMask) {
     UITableViewCellStateDefaultMask                     = 0,
     UITableViewCellStateShowingEditControlMask          = 1 << 0,
     UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1
     };
     */

    //滑動出現的刪除按鈕state是2的,編輯狀態下的刪除按鈕state是3的
    if (state == UITableViewCellStateShowingDeleteConfirmationMask||state==3) {
        for (UIView *subview in self.subviews) {
            //cell的subview爲UITableViewCellDeleteConfirmationControl時,代表是刪除按鈕
            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
                
                UIView *deleteButtonView = subview;
                CGRect f = deleteButtonView.frame;
                f.origin.x -= 50;
                deleteButtonView.frame = f;            }
        }
    }
    //插入和移動的編輯狀態state都是1
    else if(state==UITableViewCellStateShowingEditControlMask){
        for (UIView *subview in self.subviews) {
            NSString* type = @"";
            //判斷如果cell當前是插入模式,則尋找UITableViewCellEditControl的subview,代表添加按鈕
            if (self.editingStyle==UITableViewCellEditingStyleInsert) {
                type = @"UITableViewCellEditControl";
            }
            //否則尋找UITableViewCellReorderControl的subview,代表移動按鈕
            else type = @"UITableViewCellReorderControl";
            if ([NSStringFromClass([subview class]) isEqualToString:type]) {
                
                UIView *deleteButtonView = [subview.subviews objectAtIndex:0];
                CGRect f = deleteButtonView.frame;
                f.origin.x -= 50;
                deleteButtonView.frame = f; 
            }
        }

    }
}


4.UITableViewCell自定義背景顏色

方法1:

通過修改contentview的backgroundcolor


方法2:

創建一個uiview,設置它的backgroundcolor後再添加到cell裏


方法3:

通過在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath的回調中設置cell的backgroundcolor

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章