如果不是必须我肯定懒得去自定义cell

前言:cell那点事儿

原生的tableview的样式那几种或许你会觉得这根本不够用啊,然而有时候的确又没有必要再去大肆的浪费时间去创建万金油cell。这个时候我们看看源码里的两个属性。

@property (nonatomic, readonly) UITableViewCellEditingStyle editingStyle;         
// default is UITableViewCellEditingStyleNone. This is set by UITableView using the delegate's value for cells who customize their appearance accordingly.
@property (nonatomic) BOOL                            showsReorderControl;        // default is NO
@property (nonatomic) BOOL                            shouldIndentWhileEditing;   // default is YES.  This is unrelated to the indentation level below.

@property (nonatomic) UITableViewCellAccessoryType    accessoryType;              // default is UITableViewCellAccessoryNone. use to set standard type
@property (nonatomic, strong, nullable) UIView       *accessoryView;              // if set, use custom view. ignore accessoryType. tracks if enabled can calls accessory action
@property (nonatomic) UITableViewCellAccessoryType    editingAccessoryType;       // default is UITableViewCellAccessoryNone. use to set standard type
@property (nonatomic, strong, nullable) UIView       *editingAccessoryView;       // if set, use custom view. ignore editingAccessoryType. tracks if enabled can calls accessory action
25EE861C-8084-4BE8-A61F-3B88D1A3A6D1.png

如果你的项目汇总有如此的需要,只需要把右侧默认的剪头改成图片,那么你来对了。

  UIButton *btDele = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
//    btDele.backgroundColor = MTOther_Color;
[btDele setImage:[UIImage imageNamed:@"Trash"] forState:UIControlStateNormal];
btDele.userInteractionEnabled = NO;
//    [btDele addTarget:self action:@selector(deletaPassenger) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = btDele;

这里我的需求是把Button的事件禁止掉,只响应cell的,当然你也可以不取消,都是可以的。

你可以用任何UIView子类控件例如UILabel、UIButton。。。对象来给cell.accessoryView赋值,这样界面上面就会显示你想要的效果了。
设置UITableViewCell的accessoryType

如果希望cell上面显示一个浅灰色的箭头,可以通过accessoryType来达到目的,代码如下,

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
这样就设置了箭头装的type,我们可以设置多种类型,苹果定义的枚举类型如下,

typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
UITableViewCellAccessoryNone,                   //不显示任何的accessoryView
UITableViewCellAccessoryDisclosureIndicator,    //浅灰色箭头图标
UITableViewCellAccessoryDetailDisclosureButton, //显示详情的按钮
UITableViewCellAccessoryCheckmark,              //就是你考试时候打钩的钩形状
UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) //
};

读者可以逐个试一试,找到自己想要的效果。

有时候点击cell时候加深的效果

设置UITableViewCell的点击风格selectionStyle,

有的时候我们需要点击cell时候相应的cell背景加深的效果,有的时候我们不需要,这时候可以使用下面的语句来实现,

settingCell.selectionStyle = UITableViewCellSelectionStyleNone
查看文档中的枚举类型如下,

typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
};

如果这是在满足需求有可以偷懒的前提下,也算是一种方便的方式吧,欢迎交流学习。刚转移到简书来,markdown模式还是不错的,如有不足请指正。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我们在上一篇《通过代码自定义不等高cell》中学习了tableView的相关知识,本文将在上文的基础上,利用sto...
    啊世ka阅读 5,422评论 2 7
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,330评论 4 61
  • 01 高中同学小王,学习十分刻苦,最后只考上三本。 她每天学习到凌晨一两点,白天困了靠咖啡撑着,尤其是高三,咖啡是...
    木星9阅读 3,247评论 9 5
  • Hi!亲爱的小仙女,你还好吗?好像很久没有见到你了,你有没有一扣扣想我呢? 前几天,我呢,又和一个女孩恋爱了。她过...
    无限不循环F阅读 2,394评论 0 0
  • 林中,小雨婆娑,没有一点人影的晃动,静静地一片。小雨从清晨一直下到午后,没有一点停下的意思。林中,一声一只猪的叫唤...
    署者名II阅读 2,939评论 0 0