IOS UITableViewCell使用详解

简介:

IOS中UITableViewCell使用详解

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

Cell的初始化方法,可以设置一个风格和标识符,风格的枚举如下:

?
1
2
3
4
5
6
typedef  NS_ENUM(NSInteger, UITableViewCellStyle) {
     UITableViewCellStyleDefault,    // 默认风格,自带标题和一个图片视图,图片在左
     UITableViewCellStyleValue1,     // 只有标题和副标题 副标题在右边
     UITableViewCellStyleValue2,     // 只有标题和副标题,副标题在左边标题的下边
     UITableViewCellStyleSubtitle    // 自带图片视图和主副标题,主副标题都在左边,副标题在下
};


@property (nonatomicreadonlyretainUIImageView *imageView;

图片视图,风格允许时才会创建

@property (nonatomicreadonlyretainUILabel     *textLabel;

标题标签

@property (nonatomicreadonlyretainUILabel     *detailTextLabel;

副标题标签


@property (nonatomicreadonlyretainUIView      *contentView;

容纳视图,任何cell的子视图都应该添加在这个上面


@property (nonatomicretainUIView                *backgroundView;

背景视图


@property (nonatomicretainUIView                *selectedBackgroundView;

选中状态下的背景视图


@property (nonatomicretainUIView              *multipleSelectionBackgroundView;

多选选中时的背景视图


@property (nonatomicreadonlycopyNSString      *reuseIdentifier;

cell的标识符


- (void)prepareForReuse; 

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


@property (nonatomicUITableViewCellSelectionStyle   selectionStyle;  

cell被选中时的风格,枚举如下:

?
1
2
3
4
5
6
typedef  NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
     UITableViewCellSelectionStyleNone, //无
     UITableViewCellSelectionStyleBlue, //蓝色
     UITableViewCellSelectionStyleGray, //灰色
     UITableViewCellSelectionStyleDefault //默认 为蓝色
};

@property (nonatomicgetter=isSelected) BOOL         selected;  

设置cell是否选中状态


@property (nonatomicgetter=isHighlighted) BOOL      highlighted;   

设置cell是否高亮状态


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

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

与上面的两个属性对应


@property (nonatomicreadonlyUITableViewCellEditingStyle editingStyle; 

获取cell的编辑状态,枚举如下

?
1
2
3
4
5
typedef  NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
     UITableViewCellEditingStyleNone, //无编辑
     UITableViewCellEditingStyleDelete, //删除编辑
     UITableViewCellEditingStyleInsert //插入编辑
};


@property (nonatomicBOOL                            showsReorderControl; 

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

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

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

    return YES;

}

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

    

}


@property (nonatomicBOOL                            shouldIndentWhileEditing;

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

@property (nonatomicUITableViewCellAccessoryType    accessoryType; 

设置附件视图的风格(cell最右侧显示的视图) 枚举如下:

?
1
2
3
4
5
6
7
typedef  NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
     UITableViewCellAccessoryNone,                    // 没有视图
     UITableViewCellAccessoryDisclosureIndicator,     // cell右侧显示一个灰色箭头
     UITableViewCellAccessoryDetailDisclosureButton,  // 显示详情符号和灰色箭头
     UITableViewCellAccessoryCheckmark,               // cell右侧显示蓝色对号
     UITableViewCellAccessoryDetailButton   // cell右侧显示一个详情符号
};

@property (nonatomicretainUIView                 *accessoryView;  

附件视图


@property (nonatomicUITableViewCellAccessoryType    editingAccessoryType; 

cell编辑时的附件视图风格


@property (nonatomicretainUIView                 *editingAccessoryView;  

cell编辑时的附件视图


@property (nonatomicNSInteger                       indentationLevel; 

设置内容区域的缩进级别

@property (nonatomicCGFloat                         indentationWidth; 

设置每个级别的缩进宽度

@property (nonatomicUIEdgeInsets                    separatorInset;

设置分割线的偏移量


@property (nonatomicgetter=isEditing) BOOL          editing; 

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

设置是否编辑状态


@property(nonatomicreadonlyBOOL                   showingDeleteConfirmation;

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


- (void)willTransitionToState:(UITableViewCellStateMask)state;

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

- (void)didTransitionToState:(UITableViewCellStateMask)state;

cell状态已经转换时调用的函数,可以在子类中重写,状态枚举如下:

?
1
2
3
4
5
typedef  NS_OPTIONS(NSUInteger, UITableViewCellStateMask) {
     UITableViewCellStateDefaultMask                     = 0, //默认状态
     UITableViewCellStateShowingEditControlMask          = 1 << 0, //编辑状态
     UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1 //确认删除状态
};

注意:下面这些方法已经全部在IOS3.0后被废弃了,虽然还有效果,但是会被警告


@property (nonatomiccopy)   NSString *text;

设置标题

@property (nonatomicretainUIFont   *font;

设置字体

@property (nonatomicNSTextAlignment   textAlignment;

设置对其模式

@property (nonatomicNSLineBreakMode   lineBreakMode;

设置断行模式

@property (nonatomicretainUIColor  *textColor;

设置字体颜色

@property (nonatomicretainUIColor  *selectedTextColor;

设置选中状态下的字体颜色

@property (nonatomicretainUIImage  *image;

设置图片

@property (nonatomicretainUIImage  *selectedImage;

设置选中状态时的图片

@property (nonatomicBOOL              hidesAccessoryWhenEditing;

设置编辑的时候是否隐藏附件视图


目录
相关文章
|
iOS开发
iOS开发之有间距的UITableViewCell
UITableView是最常用的一个iOS控件,现要做一个如下图的UITableView,其中白色部分就是cell,可是默认的UITableView中cell之间是没有间隔的,那么办呢?网上有2种做法,我这里顺带提一下吧 效果图.png 1、方式一 通过设置cell的contentView来间接实现,在cell的contentView的顶部或者底部留下一定的间距,这样就会有cell间就有间距的效果。
1415 0
|
10月前
|
iOS开发
iOS UITableViewCell刷新某些行的cell或section
iOS UITableViewCell刷新某些行的cell或section
98 0
|
iOS开发
iOS UITableViewCell嵌套CollectionView,tableview和collectionview同时滑动bug修复
iOS UITableViewCell嵌套CollectionView,tableview和collectionview同时滑动bug修复
1045 0
|
iOS开发 开发者
iOS开发中UITableViewCell点击时子视图背景透明的解决方法
iOS开发中UITableViewCell点击时子视图背景透明的解决方法
246 0
iOS开发中UITableViewCell点击时子视图背景透明的解决方法
|
iOS开发
iOS UITableViewCell使用详解
iOS UITableViewCell使用详解
352 0

热门文章

最新文章

  • 1
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 2
    iOS|解决 setBrightness 调节屏幕亮度不生效的问题
  • 3
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 4
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 5
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 6
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 7
    iOS各个证书生成细节
  • 8
    uniapp云打包ios应用证书的获取方法,生成指南
  • 9
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
  • 10
    iOS Quartz 2D相关笔记总结笔记 韩俊强的博客
  • 1
    uniapp云打包ios应用证书的获取方法,生成指南
    29
  • 2
    iOS|解决 setBrightness 调节屏幕亮度不生效的问题
    117
  • 3
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    21
  • 4
    iOS各个证书生成细节
    36
  • 5
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    165
  • 6
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    55
  • 7
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    76
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 9
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    67
  • 10
    uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
    174