自定义cell
来源:互联网 发布:怎样在淘宝网上买东西 编辑:程序博客网 时间:2024/04/29 10:30
纯代码实现UITableViewCell的自定义扩展
(2013-11-25 12:16:36)属性
代码
功能
方法
两种
分类: iOS开发学习手记
介绍两种方法
第一种,
简单的增加UITableViewCell一些小功能
例如在cell上面添加一个UILabel。
直接在UITableViewCell的生成方法中实现,代码如下
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell
if (cell == nil) {
cell = [[[UITableViewCell
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(190, 0, 130, cell.frame.size.height)];
label1.tag = 1;
[cell.contentView addSubview:label3];
[label3 release];
}
UILabel *label3 = (UILabel *)[cell.contentView viewWithTag:1];
label1.text = @"44444";
return cell;
}
第二种,较为正规的写法。
新建一个自定义的继承UITableViewCell的类如NewCell。
在NewCell中增加两个UILabel的属性
代码如下
//NewCell.h
#import
@interface NewCell : UITableViewCell
{
UILabel *_label1;
UILabel *_label2;
}
- (void)setLabel1Text:(NSString *)text1
@end
//NewCell.m
#import "NewCell.h"
@implementation NewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
_label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160,self.frame.size.height)];
_label1.text = @"111111111";
[self.contentView addSubview:_label1];
_label2 = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 160,self.frame.size.height)];
_label2.text = @"111111111";
[self.contentView addSubview:_label2];
}
- (void)setLabel1Text:(NSString *)text1
{
_label1.text = text1;
_label2.text = text2;
}
- (void)dealloc
{
[_label1 release];
[_label2 release];
[super dealloc];
}
@end
//UITableViewCell的生成方法
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
NewCell *cell = [tableView dequeueReusableCellWithI
if (cell == nil) {
cell = [[[NewCell alloc]initWithStyle:UITableViewCellStyleDefa
}
[cell setLabel1Text:@"222222222" label2Text:@"333333333"];
return cell;
}
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- 自定义cell
- iOS cell自适应 自定义cell
- Cell报表 自定义公式
- 自定义cell另一种方法
- 使用xib自定义Cell
- 自定义Cell的用法
- iOS 自定义cell
- Matlab优化问题02—fminunc
- JavaScript权威指南_124_第15章_脚本化文档_15.4-属性-作为Attr节点的属性
- 【二十三】线索化二叉树
- HN集训比赛总结
- hashCode()和equals()
- 自定义cell
- 学习MongoDB 三: MongoDB无法启动的解决方法
- 使用java进行视频文件拼接
- 【概率论】【POJ 3682】【King Arthur's Birthday Celebration】
- Ardunio_Case_3_Homework_traffic_light
- Pranav Mistry第六感装置
- 堆与栈的区别-----超详细总结
- mysql安装图解 mysql图文安装教程(详细说明) 5.0版本
- 运行servlet时出现404错误