[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 ...
随机推荐
- IIS+WebMatrix 做 PHP 遇到的问题总结
1. URL Binding Failure Webmatrix 解决办法: 用管理员权限运行 WebMatrix 并且 改 Port. 我反正是随便输入了一个 Port 就成功了, 默认的是 610 ...
- bind: address already in use
2016/04/18 09:46:06 server.go:36: listen at 0.0.0.0:9530 2016/04/18 09:46:06 server.go:39: listen er ...
- Java命令参数说明大全
Java 在运行已编译完成的类时,是通过 java 虚拟机来装载和执行的,java 虚拟机通过操作系统命令 JAVA_HOME\bin\java –option 来启动,-option 为虚拟机参数, ...
- PostgreSQL Loadbalance Analysis CPU
Before we can even begin to decide on a processor count, we need a baseline. With a working PostgreS ...
- Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作
HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的 ...
- [原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 用Appium去操作移动设备上的chrome
最近在积极努力的学习Appium,今天成功运行了官网上的demo,在此做一个小小的总结: 前期准备工作: (1)在要运行的真机或模拟器上安装chrome. 注意:x86的虚拟机是不支持的,但是经过本人 ...
- php扩展的基本安装
phpize ./config --with-php-config=.. make&make install php.ini
- Unity中加入Android项目的Build步骤
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简介: 有的项目需要在Android中加入Unity功能,例如ANDROID应用中嵌入Un ...
- 04---Net基础加强
字符串常用方法: 属性: Length获取字符串中字符的个数 IsNullOrEmpty() 静态方法,判断为null或者为“” ToCharArray() 将string转换为char[] To ...