UITableViewCell详解

UITableViewCell:

1.使用系统自定义的各种UITableViewCell的样式

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 2     static NSString* indentifier = @"cell";
 3     MyTableCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
 4     if (!cell) {
 5         /*
 6          typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
 7          UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
 8          UITableViewCellStyleValue1,        // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
 9          UITableViewCellStyleValue2,        // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
10          UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
11          };
12          */
13         cell = [[[MyTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]autorelease];
14     }
15     cell.textLabel.text = [_data objectAtIndex:indexPath.row];
16     cell.detailTextLabel.text = @"detail";
17     cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
18     return cell;
19 }

  在UITableViewCell内默认是有contentview和accessoryView这两个subview的,contentview中的subview根据不同的cell的style会使用不同的布局。contentview和其中的默认subview会根据cell的编辑状态出现的控件自动缩进,自定义cell时可以把自定义控件添加在contentview中,也可以直接添加到cell中。

2.设置UITableViewCell的属性

 1     //cell的右边辅助按钮的样式
 2     cell.accessoryType = UITableViewCellAccessoryCheckmark;
 3     //自定义cell右边的辅助按钮
 4     cell.accessoryView = nil;
 5     //自定义cell的背景
 6     cell.backgroundView = nil;
 7     //设置cell的contentview中的detail的文字内容
 8     cell.detailTextLabel.text = @"";
 9     //查看cell当前的编辑模式
10     int style = cell.editingStyle;
11     //设置当cell进入编辑模式时的辅助按钮样式
12     cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
13     //自定义cell进入编辑模式后辅助按钮
14     cell.editingAccessoryView = nil;
15     //获取cell的缩进级别
16     int level = cell.indentationLevel;
17     //获取cell的缩进宽度
18     float width = cell.indentationWidth;
19     //设置cell被选中时的背景
20     cell.selectedBackgroundView = nil;
21     //设置cell的选中状态样式
22     cell.selectionStyle = UITableViewCellSelectionStyleBlue;
23     //设置cell的contentview中的textlabel文字内容
24     cell.textLabel.text = @"";

3.自定义的UITableViewCell重写父类的方法

 1 //初始化uitableviewcell后,自定义cell添加subview
 2 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 3 
 4 //当cell被选中时,uitableview内部会自动调用该方法,重写该方法可以在cell被选中时做一些额外的操作
 5 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
 6 
 7 //当cell处于高亮状态时,uitableview内部会自动调用该方法,重写该方法可以在cell处于高亮时做一些额外操作
 8 -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
 9 
10 //重写layoutsubviews方法,为了查看当cell改变编辑状态时,有什么subview
11 -(void)layoutSubviews{
12 
13     [super layoutSubviews];
14     NSArray* subs = self.subviews;
15     for (UIView* sub in subs) {
16         NSLog(@"view:%@",sub);
17     }
18 }

  当进入删除编辑模式时,cell的subview有一个叫UITableViewCellDeleteConfirmationControl的subview,这代表删除按钮。可以修改该view达到修改删除按钮的位置,大小等属性。

  当进入移动编辑模式时,cell的subview有一个叫UITableViewCellReorderControl的subview,这个代表移动按钮。可以修改该view达到修改移动按钮的位置,大小等属性。

  当进入插入编辑模式时,cell的subview有一个叫UITableViewCellEditControl的subview,这个代表添加按钮。可以修改该view达到修改添加按钮的位置,大小等属性。

 1 //当cell的状态变为编辑时,uitableview内部会自动调用该方法,重写该方法可以改变cell的布局
 2 -(void)willTransitionToState:(UITableViewCellStateMask)state{
 3     [super willTransitionToState:state];
 4 }
 5 //当cell的状态变为编辑时,uitableview内部会自动调用该方法,重写该方法可以改变cell的布局
 6 -(void)didTransitionToState:(UITableViewCellStateMask)state{
 7     [super didTransitionToState:state];
 8     /*
 9      typedef NS_OPTIONS(NSUInteger, UITableViewCellStateMask) {
10      UITableViewCellStateDefaultMask                     = 0,
11      UITableViewCellStateShowingEditControlMask          = 1 << 0,
12      UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1
13      };
14      */
15     //滑动出现的删除按钮state是2的,编辑状态下的删除按钮state是3的
16     if (state == UITableViewCellStateShowingDeleteConfirmationMask||state==3) {
17         for (UIView *subview in self.subviews) {
18             //cell的subview为UITableViewCellDeleteConfirmationControl时,代表是删除按钮
19             if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
20                 
21                 UIView *deleteButtonView = subview;
22                 CGRect f = deleteButtonView.frame;
23                 f.origin.x -= 50;
24                 deleteButtonView.frame = f;            }
25         }
26     }
27     //插入和移动的编辑状态state都是1
28     else if(state==UITableViewCellStateShowingEditControlMask){
29         for (UIView *subview in self.subviews) {
30             NSString* type = @"";
31             //判断如果cell当前是插入模式,则寻找UITableViewCellEditControl的subview,代表添加按钮
32             if (self.editingStyle==UITableViewCellEditingStyleInsert) {
33                 type = @"UITableViewCellEditControl";
34             }
35             //否则寻找UITableViewCellReorderControl的subview,代表移动按钮
36             else type = @"UITableViewCellReorderControl";
37             if ([NSStringFromClass([subview class]) isEqualToString:type]) {
38                 
39                 UIView *deleteButtonView = [subview.subviews objectAtIndex:0];
40                 CGRect f = deleteButtonView.frame;
41                 f.origin.x -= 50;
42                 deleteButtonView.frame = f; 
43             }
44         }
45 
46     }
47 }

4.UITableViewCell自定义背景颜色

方法1:

  通过修改contentview的backgroundcolor

方法2:

  创建一个uiview,设置它的backgroundcolor后再添加到cell里

方法3:

  通过在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath的回调中设置cell的backgroundcolor

转载于:https://www.cnblogs.com/tryingx/articles/3719456.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值