cell

添加数据源, 由三个函数来回答数据绑定的请求:numberOfSectionsInTableView, numberOfRowsInSection 和 cellForRowAtIndexPath.

用numberOfSectionsInTableView方法来返回table中有几个组.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

用numberOfRowsInSection方法来返回每个组里有几行

- (NSInteger)tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section

{

return nRecords;

}

最后用cellForRowAtIndexPath来得到一个包含每一行显示信息的UITableViewCell对象. UITableViewCell类支持文本和图像,编辑和删除确认等功能. 这些信息会保存在表队列里,用来至此翻页等功能,但是内存很低的时候会自动释放,然后再需要的时候重新创建.

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *CellIdentifier = [ [ NSString alloc ] initWithFormat:

@"Cell %d", [ indexPath indexAtPosition: 1 ] ];

UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier ];

if (cell == nil) {

cell = [ [ [ UITableViewCell alloc ]

initWithFrame: CGRectZero reuseIdentifier: CellIdentifier ]

autorelease

];

}

cell.text = CellIdentifier;

return cell;

}

NSIndexPath用来保存哪一组的哪一行.

[ indexPath indexAtPosition: 0 ]哪一组

[ indexPath indexAtPosition: 1 ]哪一行

7.2 UITableViewCell包含图像,文本等.

NSString *CellIdentifier = [ [ NSString alloc ] initWithString: @"Frank" ];

UITableViewCell *cell = [ [ [ UITableViewCell alloc ]

initWithFrame: CGRectZero

reuseIdentifier: CellIdentifier

] autorelease

];

(3) 字体和尺寸:

#import <UIKit/UIFont.h>

UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 18.0 ];

cell.font = myFont;

//系统字体

UIFont *mySystemFont = [ UIFont systemFontOfSize: 12.0 ];

UIFont *myBoldSystemFont = [ UIFont boldSystemFontOfSize: 12.0 ];

UIFont *myItalicSystemFont = [ UIFont italicSystemFontOfSize: 12.0 ];

(4) 颜色

#import <UIKit/UIColor.h>

//文本颜色

cell.textColor = [ UIColor redColor ];

//当前选择项的颜色

cell.selectedTextColor = [ UIColor blueColor ];

(5) 图像

//从你应用程序目录下的文件创建一个image

cell.image = [ UIImage imageNamed: @"cell.png" ];

//当前选中项的图形

cell.selectedImage = [ UIImage imageNamed: @"selected_cell.png" ];

可以修改table保准行高来适应你的图形高度

- (id)init

{

self = [ super init ];

if (self != nil) {

self.tableView.rowHeight = 65;

}

return self;

}

你也可以为每一个cell定义不同的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if ([ indexPath indexAtPosition: 1 ] == 0)

return 65.0;

else

return 40.0;

}

(6)选中项的风格

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

UITableViewCellSelectionStyleBlue 默认选中项是蓝色

UITableViewCellSelectionStyleGray 灰色

UITableViewCellSelectionStyleNone 没有变化

(7)标签 (labels)

在偏移量100x0处创建一个尺寸50x50 label:

UILabel *label = [ [ UILabel alloc ] initWithFrame: CGRectMake(100.0, 0.0, 50.0, 50.0) ];

label.text = @"Label Text";

label.textAlignment = UITextAlignmentLeft;

label.textColor = [ UIColor redColor ];

label.font = [ UIFont fontWithName: @"Arial" size: 10.0 ];

标签label可以设置文本阴影,甚至可以定义阴影的偏移:

label.shadowColor = [ UIColor grayColor ];

label.shadowOffset = CGSizeMake(0, -1);

高亮是的颜色:

label.highlightedTextColor = [ UIColor blackColor ];

标签的背景色:

label.backgroundColor = [ UIColor blueColor ];

把标签加到cell里

[ cell addSubview: label ];

(8) 附件

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

Style

Description

UITableViewCellAccessoryNone

没有附件

UITableViewCellAccessoryDisclosureIndicator

黑色向右的箭头

UITableViewCellAccessoryDetailDisclosureButton

蓝色附件按钮

UITableViewCellAccessoryCheckmark

复选框,支持选择

7.3 实现多选

- (void)tableView:(UITableView *)tableView

didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"Selected section %d, cell %d",

[ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition: 1 ]);

//获的当前选择项

UITableViewCell *cell = [ self.tableView cellForRowAtIndexPath: indexPath ];

//显示复选框

if (cell.accessoryType == UITableViewCellAccessoryNone)

cell.accessoryType = UITableViewCellAccessoryCheckmark;

else

cell.accessoryType = UITableViewCellAccessoryNone;

}

7.4 编辑和删除

在允许用户删除和编辑的时候,每一个cell左边会显示一个红色删除图标

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

关闭编辑的时候,table顶部会显示一个Edit导航条

[ self.tableView setEditing: NO animated: YES ];

在编辑过程中,如果用户要删除该项,会弹出一个删除确认框. 确认后调UITableViewDataSource类的commitEditingStyle方法来通知你的应用程序, 然后你可以从你的底层数据源里删除该项,并通知table view删除该行.

- (void)tableView:(UITableView *)tableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *) indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete)

{

NSLog(@"Deleted section %d, cell %d", [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition: 1 ]);

NSMutableArray *array = [ [ NSMutableArray alloc ] init ];

[ array addObject: indexPath ];

[ self.tableView deleteRowsAtIndexPaths: array

withRowAnimation: UITableViewRowAnimationFade

];

}

}

通过传递一个数组给deleteRowsAtIndexPaths方法, 可以删除一行或多行.

withRowAnimation至此下列预定义的删除动画

Animation

Description

UITableViewRowAnimationFade

Cell fades out

UITableViewRowAnimationRight

Cell slides out from right

UITableViewRowAnimationLeft

Cell slides out from left

UITableViewRowAnimationTop

Cell slides out to top of adjacent cell

UITableViewRowAnimationBottom

Cell slides out to bottom of adjacent cell

7.5 重新加载表

当你的数据变了的时候,你可以重新加载整个表

[ self.tableView reloadData

时间: 2024-08-03 09:43:52

cell的相关文章

UI_11 自定义UITableViewCell、Cell的高度自适应

UITableViewCell很难满足我们的需求,因此,CustomCell(自定义单元格)至关重要.下面将通过一个例子演示自定义Cell.第二部分演示根据文本内容自适应Label.Cell高度. 第一部分 CustomCell的创建 1.创建DemoTableViewController,继承自UITableViewController,并设置其为window的根视图 AppDelegate.m - (BOOL)application:(UIApplication *)application

UITableViewCell中的使用cell和cell.contentView的区别

一般我们向cell中添加子视图,有两种方式 1.[cell addSubview:] 2.[cell.contentView addSubview:] 区别在于进行cell编辑时,比如cell内容向左移或者右移时,第一种方式子视图不会移动,第二可以,所以这种情况一般使用第二种方式. 还有在设置backgroundColor时,使用cell设置时左移或者右移颜色是不会变的,而用cell.contentCell设置时,移动后的空白会显示cell的默认颜色,这种情况视实际情况选择. 其实这两种方式在大

tableView cell性能优化

通过一个标识表去缓冲池中寻找可循环利用的cell 如果缓存池找不到可循环利用的cell,创建一个新的cell,给cell贴个标识 给cell设置新的数据 代码如下cellForRowAtIndexPath方法中 //dequeue查找队列 //cell标识,static修饰局部变量:可以保证局部变量只分配一次存储空间 static NSString *ID = @"A"; UITableViewCell *cell = [tableView dequeueReusableCellWit

cell的imageVIew的fram问题

今天你在输出cell的imageVIew的fram时,发现新建的cell的imageVIew的frame是(0,0,0,0),但是重用的cell的imageVIew的frame输出是(15,19,30,30)(code6,iphone6模拟器),后又添加了不同的图片发现测试发现重用的cell.fame.origin.x一直是15,其他的会根据图片不同而改变. 新建的cell的frame是(0,0,320,44),但是重用的cell的imageVIew的frame输出是(4,递增,375,自定义的

改变cell的背景颜色

#define DARK_BACKGROUND  [UIColor colorWithRed:151.0/255.0 green:152.0/255.0 blue:155.0/255.0 alpha:1.0]; - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    cell.backgroun

IOS xib在tableview上的简单应用(通过xib自定义cell)

UITableView是一种常用的UI控件,在实际开发中,由于原生api的局限,自定义UITableViewCell十分重要,自定义cell可以通过代码,也可以通过xib. 这篇随笔介绍的是通过xib自定义cell. 首先通过gif介绍如何创建xib. 然后实现代码部分,要注意的是实现代码的同时要使代码与xib相关联.-如图 下面便是代码,一些解释我在代码中注释了. ViewController.m // // ViewController.m // CX-Xib在tableView中的简单应用

IOS 通过 代码 自定义cell(Cell的高度不一致)(优化性能)

创建cell的步骤 1.新建一个继承自UITabelViewCell的类 2.重写 initWithStyle:ReuseIdentifier: 方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame,子控件要添加到contentView中) 进行子控件一次性的属性设置(有些属性中需要设置一次,比如字体\固定的图片) 3.提供2个模型 数据模型:存放文字数据\图片数据 frame模型:存放数据模型\所有子控件的frame\cell的高度(可优化性能) 4.cell拥有一个frame模

xib自定义cell代码规范

// //  MJTgCell.m //  01-团购 // //  Created by apple on 14-4-1. //  Copyright (c) 2014年 itcast. All rights reserved. // #import "MJTgCell.h" #import "MJTg.h" @interface MJTgCell() @property (weak, nonatomic) IBOutlet UIImageView *iconVi

新浪微博客户端(22)-创建微博Cell

DJStatusCell.h #import <UIKit/UIKit.h> @class DJStatusCellFrame; @interface DJStatusCell : UITableViewCell /** DJStatusCell 的默认构造方法 */ + (instancetype)cellWithTableView:(UITableView *)tableView; @property (nonatomic,strong) DJStatusCellFrame *status

使用Aspose.Cell控件实现Excel高难度报表的生成(一)

时光飞逝,生活.工作.业余研究总是在不停忙碌着,转眼快到月底,该月的博客文章任务未完,停顿回忆一下,总结一些经验以及好的东西出来,大家一起分享一下.本文章主要介绍报表的生成,基于Aspose.Cell控件的报表生成.谈到报表,估计大家都有所领悟以及个人的理解,总的来说,一般的报表生成,基本上是基于以下几种方式:一种是基于微软Excel内置的引擎来实现:一种是构造HTML格式的Excle报表:一种是基于控件的方式来处理,基于控件有很多种方式,个人认为比较有名的是Aspose.Cell(收费破解)和