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 (nonatomic, readonly, retain) UIImageView *imageView;

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

@property (nonatomic, readonly, retain) UILabel     *textLabel;

标题标签

@property (nonatomic, readonly, retain) UILabel     *detailTextLabel;

副标题标签

@property (nonatomic, readonly, retain) UIView      *contentView;

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

@property (nonatomic, retain) UIView                *backgroundView;

背景视图

@property (nonatomic, retain) UIView                *selectedBackgroundView;

选中状态下的背景视图

@property (nonatomic, retain) UIView              *multipleSelectionBackgroundView;

多选选中时的背景视图

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

cell的标识符

- (void)prepareForReuse; 

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

@property (nonatomic) UITableViewCellSelectionStyle   selectionStyle;  

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

?


1

2

3

4

5

6

typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {

    UITableViewCellSelectionStyleNone,//无

    UITableViewCellSelectionStyleBlue,//蓝色

    UITableViewCellSelectionStyleGray,//灰色

    UITableViewCellSelectionStyleDefault//默认 为蓝色

};

@property (nonatomic, getter=isSelected) BOOL         selected;  

设置cell是否选中状态

@property (nonatomic, getter=isHighlighted) BOOL      highlighted;   

设置cell是否高亮状态

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

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

与上面的两个属性对应

@property (nonatomic, readonly) UITableViewCellEditingStyle editingStyle; 

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

?


1

2

3

4

5

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

    UITableViewCellEditingStyleNone,//无编辑

    UITableViewCellEditingStyleDelete,//删除编辑

    UITableViewCellEditingStyleInsert//插入编辑

};

@property (nonatomic) BOOL                            showsReorderControl; 

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

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

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

    return YES;

}

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

    

}

@property (nonatomic) BOOL                            shouldIndentWhileEditing;

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

@property (nonatomic) UITableViewCellAccessoryType    accessoryType; 

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

?


1

2

3

4

5

6

7

typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {

    UITableViewCellAccessoryNone,                   // 没有视图

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

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

    UITableViewCellAccessoryCheckmark,              // cell右侧显示蓝色对号

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

};

@property (nonatomic, retain) UIView                 *accessoryView;  

附件视图

@property (nonatomic) UITableViewCellAccessoryType    editingAccessoryType; 

cell编辑时的附件视图风格

@property (nonatomic, retain) UIView                 *editingAccessoryView;  

cell编辑时的附件视图

@property (nonatomic) NSInteger                       indentationLevel; 

设置内容区域的缩进级别

@property (nonatomic) CGFloat                         indentationWidth; 

设置每个级别的缩进宽度

@property (nonatomic) UIEdgeInsets                    separatorInset;

设置分割线的偏移量

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

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

设置是否编辑状态

@property(nonatomic, readonly) BOOL                   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 (nonatomic, copy)   NSString *text;

设置标题

@property (nonatomic, retain) UIFont   *font;

设置字体

@property (nonatomic) NSTextAlignment   textAlignment;

设置对其模式

@property (nonatomic) NSLineBreakMode   lineBreakMode;

设置断行模式

@property (nonatomic, retain) UIColor  *textColor;

设置字体颜色

@property (nonatomic, retain) UIColor  *selectedTextColor;

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

@property (nonatomic, retain) UIImage  *image;

设置图片

@property (nonatomic, retain) UIImage  *selectedImage;

设置选中状态时的图片

@property (nonatomic) BOOL              hidesAccessoryWhenEditing;

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

时间: 2024-08-26 07:43:00

IOS UITableViewCell使用详解的相关文章

iOS UIView动画详解(Objective-C)

    我在之前的一篇博客中<iOS UIView动画详解(Swift)>讲解了使用Swift来实现UIView类下面提供的多种动画效果,如位置动画.旋转动画.缩放动画.颜色动画.透明度动画等等.为了这个题目的完整性,今天我使用Objective-C来完全重写以上的所有的动画.项目案例已经上传至:https://github.com/chenyufeng1991/iOS-UIView-Animation  中的Animation-OC文件夹下,另一个目录下则是Swift实现的动画. (1)位置

IOS 指纹识别详解及实例代码_IOS

IOS 指纹识别,这里整理下项目中用的知识. IOS 指纹识别现在,在要求安全与效率兼得的时候,普通密码已不能满足我们的要求,指纹识别就这样诞生了. 每个人都有自己专属的指纹,在需要支付等输入密码的地方,我们只需轻轻一按即可,避免了输入密码的繁琐步骤,更加安全,而且妈妈再也不用担心我们忘记密码. 其实,听着高大上,实现起来特别简单,因为苹果已经帮我们封装好了,我们只需要简单的调用就好了. 1.首先,我们需要导入头文件: #import <LocalAuthentication/LocalAuth

iOS开发笔记--详解UILabel的相关属性设置_IOS

在iOS编程中UILabel是一个常用的控件,下面分享一下UILabel的相关属性设置的方法. 很多学习iOS6编程都是从storyboard开始,用到UILabel时是将控件拖到storyboard中生成实现,如果想要在-(void)viewDidLoad中用代码如[_label initWithFrame:CGRectMake(X,Y,WIDTH,HEIGHT)]方法改变拖拽到storyboard的label的大小是行不通的,因为程序加载时先执行了-(void)viewDidLoad的代码,

IOS 多线程GCD详解_IOS

Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispatch_get_main_queue获取. #definedispatch_get_main_queue() \DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q) 可以看出,dispatch_get_main_queue也是一种disp

iOS开发之详解正则表达式

  正则表达式(广为所知的"regex")是一个字符串或一个字符序列来说明一种模式,把它作为一个搜索字符串-非常强大! 在一个文本编辑器或文字处理器中普通的旧式搜索只允许你进行简单的匹配.正则表达式可以实现这样简单的搜索,它还能让你更进一步地按模式搜索,例如,在两个数字后跟一个字母,或者,三个字母后跟一个连字符. 这种模式匹配能让你做更有用的事,如验证字段(电话号码,邮箱地址),检查用户输入,执行更高级的文本操作等等. 如果你渴望了解更多关于正则表达式在iOS中的用法,看一些本教程之外

iOS之Block详解

一.Block定义 二.Block原理 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // main.m   int main(int argc, const char * argv[]) {     @autoreleasepool {           //        static int age = 20;         __block int age = 20;                   void (^blco

Cisco asa 5510升级IOS和ASDM详解

本文主要从技术层面向大家详细的介绍了如何去升级IOS和ASDM,并且文章给出了具体的配置过程,详细通过此文使你有意匪浅.show version 查看当前运行的系统信息,包括启动文件(即IOS)等show boot 查看当前的IOS信息show asdm image 查看当前运行的ASDM信息copy nvram:/filename tftp://ip/filename 用tftp协议进行文件传输boot system file 设置IOS启动文件asdm image file 设置asdm启动

iOS UIAppearance使用详解

  iOS5及其以后提供了一个比较强大的工具UIAppearance,我们通过UIAppearance设置一些UI的全局效果,这样就可以很方便的实现UI的自定义效果又能最简单的实现统一界面风格,它提供如下两个方法. + (id)appearance 这个方法是统一全部改,比如你设置UINavBar的tintColor,你可以这样写:[[UINavigationBar appearance] setTintColor:myColor]; + (id)appearanceWhenContainedI

IOS UIButton使用详解

第一.UIButton的定义       UIButton *button=[[UIButton buttonWithType:(UIButtonType); 能够定义的button类型有以下6种,  typedef enum {  UIButtonTypeCustom = 0, 自定义风格  UIButtonTypeRoundedRect, 圆角矩形   UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用  UIButtonTypeInfoLight,