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

猜你喜欢

转载自blog.csdn.net/Smile_chunyang/article/details/51858735