加载本地UITableViewCell
来源:互联网 发布:java并发编程 豆瓣 编辑:程序博客网 时间:2024/06/04 19:25
转自:非代码方式创建UItableViewCell
我们很多时候都是通过代码的方式alloc,init一个uitableviewcell,可是有的时候却有那么一点必要来使用xib文件所见即所得的来设计布局我们的cell。下面介绍如何使用非代码的方式创建他们。
方法一:
新建一个空的nib文件,拖一个uitableviewcell到视图中。如果有必要就修改所属类为你的自定义类,拖拽各种属性关系。
然后在加载的时候使用如下代码
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@”YOUNIBFILENAME” owner:nil options:nil];for(id currentObject in topLevelObjects)//其实一般都是objectAtIndex 0{ if([currentObject isKindOfClass:[iCodeBlogCustomCell class]]) { cell = (iCodeBlogCustomCell *)currentObject; break; }}
说明:参考一中说代码生成的更快,可是我测试的时候是nib加载的更快。
方法二:使用UINib
在需要使用这个cell的类中IBOutlet 一个这个cell的对象。只需要读取一次,然后放在内存中,所以速度会很快。
IBOutlet UITableViewCell *loadedCell;
新建一个空的nib文件,拖一个uitableviewcell到视图中。如果有必要就修改所属类为你的自定义类,拖拽各种属性关系。
然后修改File's Owner为你需要使用这个cell的类。关联IBOutlet到这个cell上。
需要加载cell时使用如下代码
[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];cell = loadedCell;loadedCell = nil;
或者这样的代码
.h文件中
@property(nonatomic,strong) UINib* topicCellNib;
@property(nonatomic,assign) IBOutletTopicCell* topicCell;
。m文件
- (UINib *)countryCellNib{ if (!_countryCellNib) { _countryCellNib = [UINib nibWithNibName:@"CountryCell" bundle:nil]; } return _countryCellNib;} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UYLCountryCellIdentifier]; if (cell == nil) { [self.countryCellNib instantiateWithOwner:self options:nil]; cell = self.countryCell; self.countryCell = nil; } // Code omitted to configure the cell... return cell;}
方法三:
iOS5中,可以为tableview注册一个nib绑定cell
- (void)viewDidLoad{ ... ... UINib *countryNib = [UINib nibWithNibName:@"CountryCell" bundle:nil]; [self.tableView registerNib:countryNib forCellReuseIdentifier:UYLCountryCellIdentifier];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UYLCountryCellIdentifier]; // Code omitted to configure the cell... return cell;}
方法四:
静态table view
如果使用storyboard并且创建的是uitablewviewController的子类,那么可以使用静态表视图,事先就完全定义好各个cell,这次你在写程序那个让人崩溃的繁杂的更多页面是不是顿时轻松了许多?具体示例见参考二。
注:静态的表视图只能用在UITableViewController子类中。
方法五:
使用storyboard,
拖个tableview到视图上,
拖个tableviewcell到tableview上,
设置tableview的代理,
设置tableviewcell的标示符
在cellforrollatindexpath中直接dequeue就可以了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString* identifier = @"cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier]; cell.textLabel.text = @"12313"; return cell;}
可以添加多个不同样式的cell,用的时候根据不同的identifier查找就可以了。
类似的,可以用nib方式来加载UIView。
参考一:http://cocoawithlove.com/2010/03/load-from-nib-or-construct-views-in.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+CocoaWithLove+%28Cocoa+with+Love%29
参考二:http://useyourloaf.com/blog/2012/05/07/static-table-views-with-storyboards.html
参考三:http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-uitableview-tutorial.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FSoFHn+%28Technology+Blog%29&utm_content=Google+Reader
参考四:http://useyourloaf.com/blog/2012/06/07/prototype-table-cells-and-storyboards.html
- 加载本地UITableViewCell
- UITableViewCell 图片加载
- UITableViewCell加载图片
- iphone(UITableViewCell)动态加载图片
- iphone(UITableViewCell)动态加载图片
- iphone(UITableViewCell)动态加载图片
- UITableViewCell派生类加载Xib
- UITableViewCell Storyboard加载错误解决
- Xib设计UITableViewCell然后动态加载
- 从nib文件加载自定义的UITableviewCell
- Xib设计UITableViewCell然后动态加载
- 通过xib加载UITableViewCell的新方式
- 从nib文件加载自定义的UITableviewCell
- 通过xib加载UITableViewCell的新方式
- registerNib:forCellReuseIdentifier: 加载自定义的UITableViewCell
- 重用从xib加载的UITableViewCell
- UITableViewCell加载时的简单动画效果
- 通过xib加载UITableViewCell的新方式
- sqoop 安装及使用
- 备忘录模式
- Js 错误 - Throw、Try 和 Catch
- WPF Image控件显示视频数据流
- 楠哥讲心得之三维重建
- 加载本地UITableViewCell
- 基于TinyProxy搭建HTTP代理服务器
- 九九乘法表
- Web Workers
- Matlab中的符号变量和符号运算
- web工程前后台验证(后台合法性校验和页面元素校验)统一处理
- 数据挖掘及相关应用
- CSS3概述
- 设计模式___策略模式