打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
tableview中cell的设置

  7.1 创建表

(1) 创建一个UITableViewController的子类

@interface MyTableViewController : UITableViewController {}-(id)init; -(void)dealloc;


添加数据源, 由三个函数来回答数据绑定的请求: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];


然后你可以为每一个cell设置不同的风格

(1) 显示文本: cell.text = @"Frank's Table Cell";

(2) 对齐: cell.textAlignment = UITextAlignmentLeft;

UITextAlignmentLeft 默认是左对齐

UITextAlignmentRight 右对齐

UITextAlignmentCenter 中对齐

(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 ];

  7.1 创建表

(1) 创建一个UITableViewController的子类

@interface MyTableViewController : UITableViewController {}-(id)init; -(void)dealloc;


添加数据源, 由三个函数来回答数据绑定的请求: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];


然后你可以为每一个cell设置不同的风格

(1) 显示文本: cell.text = @"Frank's Table Cell";

(2) 对齐: cell.textAlignment = UITextAlignmentLeft;

UITextAlignmentLeft 默认是左对齐

UITextAlignmentRight 右对齐

UITextAlignmentCenter 中对齐

(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 ];


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
可任意自定义的UITableViewCell
iOS开发UI篇—UITableview控件基本使用
UITableViewCell详解
UITableViewCell自定义右边的文字以及更改左边图片的大小
转:cellForRowAtIndexPath 的标准用法
关于tableviewcell addsubview的重叠问题 | iOS开发
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服