UITableViewCell

一、简介

<<继承关系:UITableViewCell --> UIView -->UIResponder-->NSObject

格式为

1--> 设置UITableViewCell选中的样式(属性的作用)

typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {

    UITableViewCellSelectionStyleNone,

    UITableViewCellSelectionStyleBlue,

    UITableViewCellSelectionStyleGray,

    UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)

};(如果属性有枚举类型的话,这里会有枚举类型说明)

tableViewCell.selectionStyle = UITableViewCellSelectionStyleDefault;(这是具体的例子)

@property (nonatomic) UITableViewCellSelectionStyle selectionStyle; // default is UITableViewCellSelectionStyleDefault.(这是属性的说明)

二、UITableViewCell的初始化方法(属性的顺序与苹果API一致)

1-->初始化UITableViewCell

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:cellIdentifier];//初始化方法,一般都是复用的时候调用

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier NS_AVAILABLE_IOS(3_0) NS_DESIGNATED_INITIALIZER;

2-->初始化UITableViewCell

- (instancetype)initWithCoder:(NSCoder*)coder{

self= [superinitWithCoder:coder];

if(self) {

NSLog(@"%s %@",__func__,NSStringFromCGRect(self.frame));

 }

returnself;

}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

三、UITableViewCell的属性

1-->设置UITableViewCell的图片

tableViewCell .imageView.image = image;

@property (nonatomic, readonly, strong, nullable) UIImageView *imageView NS_AVAILABLE_IOS(3_0); // 默认是空,如有需要,将创建图像视图,图片视图,风格允许时才会创建

2、设置UITableViewCell的标题

tableViewCell .textLabel.text = self.yearArr[indexPath.row];

@property (nonatomic, readonly, strong, nullable) UILabel *textLabel NS_AVAILABLE_IOS(3_0); //默认是空,如有需要,将创建标题视图。

3、设置UITableViewCell的副标题

tableViewCell .detailTextLabel.text = self.groups[indexPath.section].teams[indexPath.row].name;

@property (nonatomic, readonly, strong, nullable) UILabel *detailTextLabel NS_AVAILABLE_IOS(3_0);//默认是空,如有需要,将创建文本视图。

4、UITableViewCell的容纳视图

cell.contentView.backgroundColor = [UIColor yellowColor];

@property (nonatomic, readonly, strong) UIView *contentView;//容纳视图,任何cell的子视图都应该添加在这个上面

5、UITableViewCell的背景视图

cell.backgroundView.backgroundColor=[UIColor redColor];

@property (nonatomic, strong, nullable) UIView *backgroundView;//自定义未选择下cell的背景

6、UITableViewCell的选中状态下的背景视图

 cell.selectedBackgroundView.backgroundColor=[UIColor redColor];

@property (nonatomic, strong, nullable) UIView *selectedBackgroundView;//自定义未选择下cell的背景

备注:1、cell的backgroundView优先级>backgroundColor 

            2、backGroundView为cell的最底层,而selectedBackgroundView则相反,位于最顶层。 也就是说,如果你在cell上(一般会在其contentView)堆放了如何的不透明的view,则backGroundView都会被覆盖,而selectedBackgroundView在选中的情况下会覆盖你在cell中堆放的view。

           3、默认情况下,backgroundView和selectedBackgroundView在UITableViewStylePlain下的为空, UITableViewStyleGrouped下为非空。

7、UITableViewCell的多选选中时的背景视图

  cell.multipleSelectionBackgroundView.backgroundColor=[UIColor redColor];

@property (nonatomic, strong, nullable) UIView *multipleSelectionBackgroundView NS_AVAILABLE_IOS(5_0);

8、cell的标识符

static NSString *CellIdentifier = @"TimeLineViewCell";

TimeLineViewCell *cell = (TimeLineViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell==nil){

    cell = [[TimeLineViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier11"];

}//添加复用标示

@property (nonatomic, readonly, copy, nullable) NSString *reuseIdentifier;

9、当被重用的cell将要显示时触发

-(void)prepareForReuse

{

  [super prepareForResuse];//必须要加

    for(UIView *viewin _imgViews.subviews) {

        if(view.tag >0&& ([view isKindOfClass:[UIImageViewclass]]||[view isKindOfClass:[UILabelclass]])) {

            [view removeFromSuperview];

        }

    }

}

- (void)prepareForReuse  NS_REQUIRES_SUPER;

备注:

    1、当被重用的cell将要显示时,会调用这个方法,这个方法最大的用武之地是当你自定义的cell上面有图片时,如果产生了重用,图片可能会错乱(当图片来自异步下载时及其明显),这时我们可以重写这个方法把内容抹掉。

    2、在tableView调用dequeueReusableCellWithIdentifier:之前调用,如果要重载则必须调用[super prepareForReuse];

10、设置UITableViewCell选中时的样式

typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {

    UITableViewCellSelectionStyleNone,//无

    UITableViewCellSelectionStyleBlue,//蓝色

    UITableViewCellSelectionStyleGray,//灰色

    UITableViewCellSelectionStyleDefault//默认 为蓝色NS_ENUM_AVAILABLE_IOS(7_0)

};

tableViewCell.selectionStyle = UITableViewCellSelectionStyleDefault;

@property (nonatomic) UITableViewCellSelectionStyle selectionStyle; // 默认UITableViewCellSelectionStyleDefault.

11、设置UITableViewCell是否选中状态

tableViewCell.selected=YES;

@property (nonatomic, getter=isSelected) BOOL selected;//设置选定的状态(标题、图像、背景)。默认是NO,没有动画

12、设置cell是否高亮状态

 tableViewCell.highlighted=YES;

@property (nonatomic, getter=isHighlighted) BOOL      highlighted;  //设置突出显示的状态(标题、图像、背景)。默认是NO ,没有动画

13、与上面的两个属性对应,只不过加了动画

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [supersetSelected:selected animated:animated];

 _unreadBadge.backgroundColor = [UIColorredColor];

}

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

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [supersetHighlighted:highlighted animated:animated];

 _unreadBadge.backgroundColor = [UIColorredColor];

}

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

参见:点击UITableViewCell发生了什么?

14、获取cell的编辑状态

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

    UITableViewCellEditingStyleNone,//无编辑

    UITableViewCellEditingStyleDelete,//删除编辑

    UITableViewCellEditingStyleInsert//插入编辑

};

    switch (cell.editingStyle) {

        case UITableViewCellEditingStyleNone:

            break;

        case UITableViewCellEditingStyleDelete:

            break;

        case UITableViewCellEditingStyleInsert:

            break;

        default:

            break;

    }

@property (nonatomic, readonly) UITableViewCellEditingStyle editingStyle; //只读属性

15、设置是否显示cell自带的自动排序控件

cell.showsReorderControl = YES;//启用编辑模式需要设置为yes  

@property (nonatomic) BOOL showsReorderControl; // 默认是 NO

注意:要让cell实现拖动排序的功能,除了上面设置为YES,还需实现代理中的如下方法:

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

 return YES;

}

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


}

16、设置编辑状态下是否显示缩进

cell.shouldIndentWhileEditing=YES;//开启缩进

@property (nonatomic) BOOL shouldIndentWhileEditing;//默认是yes。这与下面的缩进级别无关。

17、设置附件视图的风格(cell最右侧显示的视图) 

typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {

 UITableViewCellAccessoryNone, 

 // 没有视图    

UITableViewCellAccessoryDisclosureIndicator,  

 // cell右侧显示一个灰色箭头   

 UITableViewCellAccessoryDetailDisclosureButton, 

// 显示详情符号和灰色箭头    

UITableViewCellAccessoryCheckmark,  

 // cell右侧显示蓝色对号    

UITableViewCellAccessoryDetailButton 

 // cell右侧显示一个详情符号

};

cell.accessoryType=UITableViewCellAccessoryNone;

@property (nonatomic) UITableViewCellAccessoryType accessoryType; //默认是UITableViewCellAccessoryNone。用于设置标准类型。

18、UITableViewCell的附件视图

 cell.accessoryView= button; 

@property (nonatomic, strong, nullable) UIView *accessoryView; //如果设置,则使用自定义视图。忽略accessoryType

参见:UITableViewCell设置accessoryType,子视图适配问题

19、设置UITableViewCell的cell编辑时的附件视图风格

typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {

 UITableViewCellAccessoryNone, 

 // 没有视图    

UITableViewCellAccessoryDisclosureIndicator,  

 // cell右侧显示一个灰色箭头   

 UITableViewCellAccessoryDetailDisclosureButton, 

// 显示详情符号和灰色箭头    

UITableViewCellAccessoryCheckmark,  

 // cell右侧显示蓝色对号    

UITableViewCellAccessoryDetailButton 

 // cell右侧显示一个详情符号

};

cell.editingAccessoryType=UITableViewCellAccessoryNone;

@property (nonatomic) UITableViewCellAccessoryType editingAccessoryType; //默认是UITableViewCellAccessoryNone。用于设置标准类型。

20、设置cell编辑时的附件视图

 cell.editingAccessoryView= button; 

@property (nonatomic, strong, nullable) UIView *editingAccessoryView; //如果设置,则使用自定义视图。忽略accessoryType

21、设置内容区域的缩进级别

 cell.indentationLevel =3;

@property (nonatomic) NSInteger indentationLevel; // 调整内容缩进。默认是0

22、设置每个级别的缩进宽度

cell.indentationWidth =5;//缩进距离为3*5=15 默认的宽度为10

@property (nonatomic) CGFloat indentationWidth; // 每个级别的宽度。默认的是10.0

23、设置分割线的偏移量

tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)

@property (nonatomic) UIEdgeInsets separatorInset;

24、设置是否编辑状态(无动画)

cell.editing=YES;

@property (nonatomic, getter=isEditing) BOOL editing; 

25、设置是否编辑状态(可以设置动画)

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

 [supersetEditing:editing animated:animated];

 [self.tableView setEditing:editing animated:animated];

self.editButtonItem.title = editing ?@"完成":@"编辑";

}

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

26、返回是否目前正在显示删除按钮

    if (self.showingDeleteConfirmation )

{

                   [self.layer removeAllAnimations];

                    break;

 }

@property(nonatomic, readonly) BOOL showingDeleteConfirmation;// 只显示“删除”按钮,只读属性

27、设置UITableViewCell的聚焦类型

typedef NS_ENUM(NSInteger, UITableViewCellFocusStyle) {

    UITableViewCellFocusStyleDefault,

    UITableViewCellFocusStyleCustom

} NS_ENUM_AVAILABLE_IOS(9_0);

cell.focusStyle=UITableViewCellFocusStyleDefault;

@property (nonatomic) UITableViewCellFocusStyle focusStyle NS_AVAILABLE_IOS(9_0) UI_APPEARANCE_SELECTOR;默认值是UITableViewCellFocusStyleDefault

28、cell状态将要转换时调用的函数,可以在子类中重写

typedef NS_OPTIONS(NSUInteger, UITableViewCellStateMask) {

    UITableViewCellStateDefaultMask                     = 0,//默认状态    UITableViewCellStateShowingEditControlMask          = 1 << 0,//编辑状态    UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1//确认删除状态

};

-(void)willTransitionToState:(UITableViewCellStateMask)state{

    [super willTransitionToState:state];

}

- (void)willTransitionToState:(UITableViewCellStateMask)state;/当cell的状态变为编辑时,uitableview内部会自动调用该方法,重写该方法可以改变cell的布局

29、cell状态已经转换时调用的函数,可以在子类中重写

typedef NS_OPTIONS(NSUInteger, UITableViewCellStateMask) {

    UITableViewCellStateDefaultMask                     = 0,//默认状态    UITableViewCellStateShowingEditControlMask          = 1 << 0,//编辑状态    UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1//确认删除状态

};

-(void)didTransitionToState:(UITableViewCellStateMask)state{

    [super didTransitionToState:state];

}

- (void)didTransitionToState:(UITableViewCellStateMask)state NS_AVAILABLE_IOS(3_0);//当cell的状态变为编辑时,uitableview内部会自动调用该方法,重写该方法可以改变cell的布局

30、通知UITableViewCell拖动状态发生了变化

- (void)dragStateDidChange:(UITableViewCellDragState)dragState{

    [super dragStateDidChange:dragState];

}

- (void)dragStateDidChange:(UITableViewCellDragState)dragState API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos, watchos);

31、设置用户在被拖动时是否可以与单元交互

cell.userInteractionEnabledWhileDragging=YES;

@property (nonatomic) BOOL userInteractionEnabledWhileDragging API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos, watchos);//默认是NO

参考

ios tableView那些事 (五) 给tableview设置缩进级别

iOS开发经验总结

iOS中UITableViewCell使用详解

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容