(转)参考: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. nsstring基本数据类型的包装类

    // //  main.m //  10-基本数据类型的包装类 // //  Created by apple on 14-3-20. //  Copyright (c) 2014年 apple. A ...

  2. openId 列表

    http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html 根据OpenID列表群发[订阅号不可用,服务号认证后可用] ...

  3. Install .NET Framework 4.5.2 on a Cloud Service Role

    October Guest OS rollout is starting today October 15 2015, and projected to be released on November ...

  4. linux:指令与档案的搜索

    linux下的五种搜索方法(参考自鸟哥linux私房菜基础篇): 一.find :功能很强大,直接搜寻整个硬碟的(速度不是很快,如果系统硬碟较旧的话)----特色:find后面可以接多个目录搜索,它本 ...

  5. Java线程同步和线程通信

    一.线程同步 当多个线程访问同一个数据时,非常容易出现线程安全问题.这时候就需要用线程同步. 不可变类总是线程安全的,因为它的对象状态是不可改变的,但可变类对象需要额外的方法来保证线程安全. 1.同步 ...

  6. ViewController 的代码规范

    1.#pragma mark - life cycle viewDidLoad viewWillAppear 2.#pragma mark - delegate #pragma mark collec ...

  7. @perproty and @synthesize

    .@property 是什么? @perperty 是声明属性的语法,他可以快速方便的为实例变量创建存取器,并允许我们通过点语法使用存取器 [存取器:用于获取和设置实例变量的方法,获取实例变量值得是g ...

  8. ant的那些闹挺事

    今日发现了一个问题,用ant+hudson去运行脚本代码时,突然ant报错: 即使我在ant的build文件中指定了basedir=“C:/Users/145064/.hudson/jobs/haix ...

  9. 3D语音天气球(源码分享)——通过天气服务动态创建3D球

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...

  10. paper 32 :svm参数优化的进展

    从今天开始,我的微信公众号“天空之窗”就暂且停止更新内容了,专心搞研究!上午从师姐那儿淘到一份关于faruto讲解的Libsvm-FarutoUltimate3.1 based on libsvm-3 ...