一、四种系统自定义 UITableVIew 样式
UITableViewCellSystemDefault UITableViewCellSystemValue1
UITableViewCellSystemValue2 UITableViewCellSystemSubtitie
二、UITableViewCell 的属性
1.cell的右边辅助按钮的样式 cell.accessoryType = UITableViewCellAccessoryCheckmark;
2.自定义cell右边的辅助按钮 cell.accessoryView = nil;
3.自定义cell的背景 cell.backgroundView = nil;
4.设置cell的contentview中的detail的文字内容 cell.detailTextLabel.text = @"";
5.查看cell当前的编辑模式 int style = cell.editingStyle;
6.设置当cell进入编辑模式时的辅助按钮样式 cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
7.自定义cell进入编辑模式后辅助按钮 cell.editingAccessoryView = nil;
8.获取cell的缩进级别 int level = cell.indentationLevel;
9.获取cell的缩进宽度 float width = cell.indentationWidth;
10.设置cell被选中时的背景 cell.selectedBackgroundView = nil;
11.设置cell的选中状态样式 cell.selectionStyle = UITableViewCellSelectionStyleBlue;
12.设置cell的contentview中的textlabel文字内容 cell.textLabel.text = @"";
三、UITableViewCell 的父类方法
1.初始化uitableviewcell后,自定义cell添加subview
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
2.当cell被选中时,uitableview内部会自动调用该方法,重写该方法可以在cell被选中时做一些额外的操作
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
3.当cell处于高亮状态时,uitableview内部会自动调用该方法,重写该方法可以在cell处于高亮时做一些额外操作
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
4.重写layoutsubviews方法,为了查看当cell改变编辑状态时,有什么subview
- (void)layoutSubviews{
[super layoutSubviews];
NSArray* subs = self.subviews;
for (UIView* sub in subs) {
NSLog(@"view:%@",sub);
}
}
a.当进入删除编辑模式时,cell的subview有一个叫UITableViewCellDeleteConfirmationControl的subview,这代表删除按钮。可以修改该view达到修改删除按钮的位置,大小等属性。
b.当进入移动编辑模式时,cell的subview有一个叫UITableViewCellReorderControl的subview,这个代表移动按钮。可以修改该view达到修改移动按钮的位置,大小等属性。
c.当进入插入编辑模式时,cell的subview有一个叫UITableViewCellEditControl的subview,这个代表添加按钮。可以修改该view达到修改添加按钮的位置,大小等属性。
5.当cell的状态变为编辑时,uitableview内部会自动调用该方法,重写该方法可以改变cell的布局
-(void)willTransitionToState:(UITableViewCellStateMask)state{ [super willTransitionToState:state]; }
6.当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; } } } }