(转)参考:http://blog.sina.com.cn/s/blog_65cbfb2b0101cd60.html

第一种,

简单的增加UITableViewCell一些小功能

例如在cell上面添加一个UILabel。

直接在UITableViewCell的生成方法中实现,代码如下

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

cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *identifier = @"cell";

UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

cell = [[[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:identifier] autorelease];

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

label2Text:(NSString *)text2;

@end

//NewCell.m

#import "NewCell.h"

@implementation NewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

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

}

return self;

}

- (void)setLabel1Text:(NSString *)text1

label2Text:(NSString *)text2

{

_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 dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

cell = [[[NewCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:identifier] autorelease];

}

[cell setLabel1Text:@"222222222" label2Text:@"333333333"];

return cell;

}

[ios]纯代码实现UITableViewCell的自定义扩展的更多相关文章

  1. iOS纯代码工程手动快速适配

    首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...

  2. iOS纯代码手动适配 分类: ios技术 2015-05-04 17:14 239人阅读 评论(0) 收藏

    首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...

  3. ios - 纯代码创建collectionView

    开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...

  4. IOS 纯代码添加 Button Image Label 添加到自定义View中

    @interface ViewController () /**获取.plist数据*/ @property (nonatomic,strong) NSArray *apps; @end @imple ...

  5. Object-C iOS纯代码布局 一堆代码可以放这里!

    前言: 最近写的文章都是创业类,好吧,今天好好写写技术类的文章! 不过分享的不是IOS相关的文章,毕竟这几天在速成IOS,看的是object-c,由于速成的很快,好累! 好在现在基本已经入了点门道了, ...

  6. Objective-C iOS纯代码布局 一堆代码可以放这里!

    前言: 最近写的文章都是创业类,好吧,今天好好写写技术类的文章! 不过分享的不是IOS相关的文章,毕竟这几天在速成IOS,看的是objective-c,由于速成的很快,好累! 好在现在基本已经入了点门 ...

  7. iOS纯代码适配masonry中mas_的问题

    //equalto 和 mas_equalto 是有区别的.但是我们不打算去了解,可以通过添加以下代码来统一. //注意!! 宏定义必须要放在 import 引入头文件之前! //define thi ...

  8. 使用纯代码定义UICollectionView和自定义UICollectionViewCell

    1.自定义UICollectionView 2.实现<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollec ...

  9. iOS 纯代码适配iPhone6,6+

    链接地址:http://blog.csdn.net/codywangziham01/article/details/37658399 转自:http://www.maxiaoguo.com/cloth ...

随机推荐

  1. bootstrap学习之一_bootstrap css

    一.文本相关样式 文本格式(用于块状标签):text-left:向左对齐文本:text-center:居中对齐文本:text-right:向右对齐文本:text-justified;text-nowr ...

  2. CL.exe的 /D 选项, Preprocessor Macro预处理器宏定义

    在看"Inside COM"第10章的代码. MAKEFILE里面有几个标记我没看懂. 去网上搜也搜不到. /D_OUTPROC_SERVER_ /DWIN32 /DREGISTE ...

  3. iOS UILabel根据文字获取高度及UITableCell动态获取高度(以截取快递信息为例)

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  4. AJAX POST&跨域 解决方案 - CORS(转载)

    跨域是我在日常面试中经常会问到的问题,这词在前端界出现的频率不低,主要原因还是由于安全限制(同源策略, 即JavaScript或Cookie只能访问同域下的内容),因为我们在日常的项目开发时会不可避免 ...

  5. spring 小结

    第一步:配置 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...

  6. 异步调用webservice

    一.异步调用 asynchronous call(异步调用):一个可以无需等待被调用函数的返回值就让操作继续进行的方法 举例: 异步调用就是你 喊 你朋友吃饭 ,你朋友说知道了 ,待会忙完去找你 ,你 ...

  7. WebService之Axis2(4):二进制文件传输

    在<WebService大讲堂之Axis2(2):复合类型数据的传递>中讲过,如果要传递二进制文件(如图像.音频文件等),可以使用byte[]作为数据类型进行传递,然后客户端使用RPC方式 ...

  8. Eclipse+SVN搭建开发环境

    目前我们开发环境为:windows service 2008 r2 x64 现在开始记录一下eclipse+SVN环境搭建过程: 1,)下载 VisualSVN-Server-3.4.2-x64:ht ...

  9. 关于prototype

    之前听过课,可是这一块没怎么听懂,最近练了两个例子,又问了问小石同学,朦朦胧胧,感觉还是不太懂,记录点心得 最基本的例子 function Box(name,age){ this.name=name; ...

  10. nyist 599 奋斗的小蜗牛

    http://acm.nyist.net/JudgeOnline/problem.php?pid=599 奋斗的小蜗牛 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 ...