[ios]纯代码实现UITableViewCell的自定义扩展
(转)参考: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的自定义扩展的更多相关文章
- iOS纯代码工程手动快速适配
首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...
- iOS纯代码手动适配 分类: ios技术 2015-05-04 17:14 239人阅读 评论(0) 收藏
首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...
- ios - 纯代码创建collectionView
开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...
- IOS 纯代码添加 Button Image Label 添加到自定义View中
@interface ViewController () /**获取.plist数据*/ @property (nonatomic,strong) NSArray *apps; @end @imple ...
- Object-C iOS纯代码布局 一堆代码可以放这里!
前言: 最近写的文章都是创业类,好吧,今天好好写写技术类的文章! 不过分享的不是IOS相关的文章,毕竟这几天在速成IOS,看的是object-c,由于速成的很快,好累! 好在现在基本已经入了点门道了, ...
- Objective-C iOS纯代码布局 一堆代码可以放这里!
前言: 最近写的文章都是创业类,好吧,今天好好写写技术类的文章! 不过分享的不是IOS相关的文章,毕竟这几天在速成IOS,看的是objective-c,由于速成的很快,好累! 好在现在基本已经入了点门 ...
- iOS纯代码适配masonry中mas_的问题
//equalto 和 mas_equalto 是有区别的.但是我们不打算去了解,可以通过添加以下代码来统一. //注意!! 宏定义必须要放在 import 引入头文件之前! //define thi ...
- 使用纯代码定义UICollectionView和自定义UICollectionViewCell
1.自定义UICollectionView 2.实现<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollec ...
- iOS 纯代码适配iPhone6,6+
链接地址:http://blog.csdn.net/codywangziham01/article/details/37658399 转自:http://www.maxiaoguo.com/cloth ...
随机推荐
- 虚拟化技术比较 PV HVM
很多人看到同样配置的VPS价格相差很大,甚是不理解,其实VPS使用的虚拟技术种类有很多,如OpenVZ.Xen.KVM.Xen和HVM与PV.在XEN中pv是半虚拟化,hvm是全虚拟化,pv只能用于L ...
- docker note from UC blog
-------------------------------------------------------------------------- original url http://tech. ...
- Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法二:设置一个任意容量的.大小合适的字节缓冲区并且使用来自文件的字节进行填充.然后整理出缓冲区 ...
- Swift游戏实战-跑酷熊猫 11 欢迎进入物理世界
物理模拟是一个奇妙的事情,以此著名的游戏有愤怒的小鸟.我们在这节将会一起来了解如何设置重力,设置物理包围体,碰撞的检测. 要点: 设置物理检测的代理: 让主场景遵循SKPhysicsContactDe ...
- Winform 基本属性
WinForm--- 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 代码部分在用户电脑上执行 WinFo ...
- [原创] 在spring 中使用quarts
1.使用maven加载 quarts 的jar <dependency> <groupId>org.quartz-scheduler</groupId> <a ...
- [转]]将 ASP.NET MVC3 Razor 项目部署到虚拟主机中
原链接:http://www.cnblogs.com/taven/archive/2011/08/14/2138077.html 国内很多网站空间都只支持.NET 2.0 和 .NET 3.0 3.5 ...
- MVC4下拉少数名族
List<SelectListItem> nationlist = new List<SelectListItem>() { new SelectListItem(){Valu ...
- mongodb聚合内存不足解决方案
原因:mongodb每一个文档默认只有16M.聚合的结果是一个BSON文档,当超过16M大小时,就会报内存不够错误. exceeded memory limit for $group.but didn ...
- MySQL的基本函数
charset(str) //返回字串字符集 mysql> select charset('demacia'); +--------------------+ | charset('demaci ...