OC开发_代码片段——使用Xib自定义tableViewCell
一、实现步骤
1、新建一个XIB文件:描述cell——tableCell.xib
2、新建UITableViewCell的子类,也就是cell文件:封装XIB内部的所有东西——TestCell.m \Testcell.h
2.1 在cell文件中拥有XIB中的所有子控件 (包括生命属性,进行连线)
2.2 给cell增加模型属性,即通过重写set方法,根据模型属性设置cell内部子控件的属性 :
(这一步是从控制器解放抽取出来放在cell中)
2.3 提供一个类方法testCell,使得返回从XIB创建好的从 cell对象(也就是mainBundle):
(这一步是从控制器解放抽取出来放在cell中)
2.4 设置一个重用标识,Identifier
(这一步是从控制器解放抽取出来放在cell中)
2.5 设置cell的高度
(这一步是从控制器解放抽取出来放在cell中)
3、修改XIB中cell的类名即Class:使得XIB和cell相关联 ——TestCell
4、新建一个模型,即数据模型,使得封装数据 ——TestModel.m\TestModel.h
5、控制器取数
5.0 设置每一个section 有多少行:numberOfRowsInsection
5.1 使用重用标示取缓存池取得cell
5.2 如果缓存池没有cell,则创建一个cell ,这个就对应上面得TestCell
5.3 传递模型给cell (这里就需要在 cell中 @class TestModel; ,也就是对应上面得2.2set方法)
二、注意点:
1、缓存为空的时候,通过nsBundle,mainBundle获得数组(两种方法)
//方法1
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:nil options:nil];
cell = objects[]; //或者:方法2
UINib *nib = [UINib nibWithNibName:@"tableCell" bundle:nil];//nil则默认为mainBundle
NSArray *array = [nib instantiateWithOwner:nil options:nil];
cell = array[];
2、cell的高度,
技巧1:代理方法
技巧2:在viewDidLoad中 self.tableView.rowheight=80(适用于每行cell的高度相同)
3、循环利用缓存中的cell,必须 在Xib的identifier中设置和代码中一样的标识(XIB适用)
4、构造模型 构造方法 :自定义构造方法:必须调用父类的构造方法
三、代码实现
TestCell.m:
#import "TestCell.h"
#import "TestModel.h"
@implementation TestCell +(id) testCell
{
return [[NSBundle mainBundle] loadNibNamed:@"tableCell" owner:nil options:nil][];
}
-(void) setModel:(TestModel *)model
{
_model = model; _descLabel.text = _model.desc;
_nameLabel.text = _model.name;
_priceLabel.text = [NSString stringWithFormat:@"%d $",_model.price];
_iconImage.image = [UIImage imageNamed:_model.icon]; } +(NSString *) getID
{
return @"cell";
} @end
TestModel.m:
#import "TestModel.h" @implementation TestModel //自定义构造方法,必须调用父类的构造方法
-(id) initWithDict:(NSDictionary *)dict
{
if(self = [super init])
{
self.desc = dict[@"desc"];
self.name = dict[@"name"];
self.icon = dict[@"icon"];
self.price = [dict[@"price"] intValue];
}
return self;
} +(id) newsWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} @end
TestTableViewController.m:
#import "TestTableViewController.h"
#import "TestCell.h"
#import "TestModel.h" @interface TestTableViewController ()
{
NSMutableArray *_data; }
@end @implementation TestTableViewController - (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = ; //加载plist文件数据数组
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil]];
_data = [NSMutableArray array];
for (NSDictionary *arr in array) {
[_data addObject:[TestModel newsWithDict:arr]];
} } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return _data.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //1、定义缓存标识
//static NSString *ID = @"cell"; //2、从缓存池取出数据
TestCell *cell = [tableView dequeueReusableCellWithIdentifier:[TestCell getID]]; //3、判断是否油缓存可以取
if (cell == nil) { cell = [TestCell testCell];
} //4、传递模型数据
cell.model = _data[indexPath.row]; return cell;
} @end
源码下载:http://pan.baidu.com/s/1kTFuHwV
OC开发_代码片段——使用Xib自定义tableViewCell的更多相关文章
- OC开发_代码片段——代码编写自定义的tableViewCell
一.介绍 之前已经实现过通过简单的XIB文件来自定义我们的tableViewCell,包括每一步的步骤和代码:http://www.cnblogs.com/daomul/p/4355999.html ...
- OC开发_代码片段——代码编写简单的tableViewCell
许久前写的简单的tableView例子,主要针对处理缓存.协议.数据源datasource.局部刷新等问题进行解析. 其实这是一篇不全面的记录,只是用来记录一些备忘的东西,更全面的是使用TablVie ...
- 36个Android开发常用代码片段
//36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...
- 安卓开发_浅谈ListView(自定义适配器)
ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果 有这样一个Demo ...
- iOS开发-代码片段(Code Snippets)提高开发效率
简介 在 XCode4 引入了一个新特性,那就是“代码片段(Code Snippets)”.对于一些经常用到的代码,抽象成模板放到 Code Snippets 中,使用的时候就只需要键入快捷键就可以了 ...
- swift开发常用代码片段
// 绑定事件 cell.privacySwitch.addTarget(self, action: #selector(RSMeSettingPrivacyViewController.switch ...
- Android开发常用代码片段
拨打电话 public static void call(Context context, String phoneNumber) { context.startActivity( new Inten ...
- OC开发_整理笔记——多线程之GCD
一.进程和线程 二.各种队列! 1.GCD:Grand Central Dispatch 2.串行队列(Serial) 你可以创建任意个数的串行队列,每个队列依次执行添加的任务,一个队列 ...
- OC开发_整理笔记——友盟分享(社交化组件)
0.友盟的地址 http://dev.umeng.com,进入友盟,在使用友盟之前我们需要注册账号获取key (1 进入我们的产品,添加新应用 (2 输入信息,然后就会获取到key 1.选择社会化分 ...
随机推荐
- 控件EditText
(一)监听EditText输入内容变化 EditText.addTextChangedListener(textWatcher); //EditText change listener //此方 ...
- BaaS后端即服务 - 概念篇
摘要: 什么是BaaS? BaaS(Backend as a Service)是一种新型的云服务,旨在为移动和Web应用提供后端云服务,包括云端数据/文件存储.账户管理.消息推送.社交媒体整合等.Ba ...
- Python 出现错误 SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform.
报出SNIMissingWarning和InsecurePlatformWarning警告. 解决方法: 在cmd中输入: pip install pyopenssl ndg-httpsclient ...
- e554. 在浏览器状态栏中显示信息
// See also e551 精简的Applet applet.showStatus("Your Message Here"); Related Examples
- MySQL无法重启问题解决Warning: World-writable config file ‘/etc/mysql/my.cnf’ is ignored
今天在修改mysql数据库的配置文件,由于方便操作,就将“/etc/mysql/my.cnf” 的权限设置成 “777” 了,然后进行修改,当修改完进行重启mysql的时候,却报错,提示Warning ...
- 转载:mysql如果数据不存在,则插入新数据,否则更新的实现方法
转自:http://www.jb51.net/article/28885.htm //如果不存在,则插入新数据 $sql = "INSERT INTO {$ecs->table(‘ca ...
- Kafka学习之一深度解析
背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能保证常数时间的访问性能 高吞吐 ...
- 浅谈session测试
Session 是用于保持状态的基于 Web 服务器的方法,在 Web 服务器上保持用户的状态信息供在任何时间从任何页访问.Session 允许通过将对象存储在 Web 服务器的内存中在整个用户会话过 ...
- mongodb group php 操作
紧接着上篇来,这篇主要讲,mongodb的group功能,做的还是挺强大的,相当对于find(),skip(),distinct()等,用法比较复杂. 测试数据 > db.fruit.find( ...
- VC++ :传统剪贴板的延迟提交技术
传统剪贴板存在的局限 传统剪贴板有一个局限性:剪贴板上的所有数据都要保存在内存上. 对于文本字符串和其它简单数据类型,可以快速有效地传递.但是,对于比较大的数据,清空剪贴板之前,数据都要占用较大的内存 ...