Creating Custom UITableViewCells with NIB files
Well this sucks. Apparently these days you can only use the Interface Builder to design your cell in XCode if you're using Storyboards. But no worries. I have found a workaround which plays very nicely in iOS5+. Let's get to it!
I'm assuming you already know so iOS and objective C, so I'll save the prep for another blog post. Let's get down to business.
You have your TableViewController.h and .m files ready. Now you want to create a sleek custom cell.
Create a new file (File -> New -> Objective C Class -> Subclass of UITableViewCell). Note that the option to create a XIB for user interface is not available. That's fine for now. Let's call this class EXCustomCell
.
This will get XCode to create 2 files - EXCustomCell.h and EXCustomCell.m.
In the EXCustomCell.h file, right below @interface line, add
@property (nonatomic, strong) IBOutlet UILabel *cellItemLabel;
@property (nonatomic, strong) IBOutlet UIImageView *cellItemImageView;
These are the properties of the cell. Add more to your liking! For every one that you want to add in IB and control programmatically, you'll need one.
I'm assuming you're using the latest version of XCode, so you won't need to @synthesize. But just in case you're not on the latest,
@synthesize cellItemImageView, cellItemLabel;
right under the @implementation line at the top of the EXCustomCell.m file.
Ok, now we've done all the setup work, time for some IB fun!
Go to File -> New, in the left hand column select User Interface, and pick View. Leave iPhone for Device Family and title the file EXCustomCell.xib
Click on the file to open up Interface Builder.
In the left hand column, under objects, click once on View and hit the delete button on your keyboard.
Drag in a Table View Cell from the Object Library
Click on File's Owner, pick the Identity Panel, and make sure that NSObject is selected as the custom class.
Click on the Cell you just dragged in and make sure its custom class is EXCustomCell
Drag in an ImageView and a Label.
This is important. Right click on the Custom cell under objects, pick the cellItemImageView and drag it over to the image, and repeat the same for cellItemLabel (except drag it to the label).
Add #import "EXCustomCell.h" to your TableViewController.m file
Now in your TableViewController.m file, at the viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"EXCustomCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCellReuseID"];
}
Note that- (void) registerNib:(UINib*)nib forCellReuseIdentifier:(NSString*)identifier;
is an iOS 5+ method. So be careful!
And in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCellReuseID";
EXCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
[cell.cellItemImage setImage:[UIImage imageNamed:@"glyphicons_428_podium"]];
[cell.cellItemLabel setText = @"Mr Burns."];
return cell;
}
That's it!! Now you can make a custom cell use a XIB file and screw those storyboard snobs!!!
除了上文提到的,还可以这样
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {staticNSString*kCellIdentifier =@"reusableCell";UITableViewCell*cell =[tableView dequeueReusableCellWithIdentifier:kCellIdentifier];if(cell == nil){[[NSBundle mainBundle] loadNibNamed:kCellIdentifier owner:self options:nil];
cell = _tableViewCell;
self.tableViewCell = nil;}return cell;}
Creating Custom UITableViewCells with NIB files的更多相关文章
- Collection View Programming Guide for iOS---(六)---Creating Custom Layouts
Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...
- 【IOS笔记】Creating Custom Content View Controllers
Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...
- ASP.NET MVC- VIEW Creating Custom HTML Helpers Part 2
The goal of this tutorial is to demonstrate how you can create custom HTML Helpers that you can ...
- Extending JMeter – Creating Custom Config Element – Property File Reader
JMeter is one of the best open source tools in the Test Automation Community. It comes with all the ...
- View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers
Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...
- [TensorFlow] Creating Custom Estimators in TensorFlow
Welcome to Part 3 of a blog series that introduces TensorFlow Datasets and Estimators. Part 1 focuse ...
- Creating Custom Connector Sending Claims with SharePoint 2013
from:http://blogs.msdn.com/b/security_trimming_in_sharepoint_2013/archive/2012/10/29/creating-custom ...
- Creating Custom Login Screen In Oracle Forms 10g
Below is the example plsql unit to validate login credentials and after successful validation open a ...
- 管理后台-第一部分:Creating custom sections in Umbraco 7 - Part 1(翻译文档)
在Umbraco上每个部分都可以被称为一个应用程序,所以这些部分和应用程序基本上是一样的.我们首先要做的事情是需要创建应用程序.在这个例子中,我不会去摆弄xml文件或是数据库——我将使用类来创建我的内 ...
随机推荐
- css响应式设计
响应式设计是指在不同分辨率的设备中,网页布局可以自适应的调整.这种弹性化的布局使网站在不同设备中的布局都比较合理,可以为不同终端的用户提供更加舒适的界面和更好的用户体验,其根本理念是使原本 PC 上的 ...
- 未注册在本地的Windows 2008 / Windows 7 X64的:'Microsoft.Jet.OLEDB.4.0'提供
在服务器上部署项目的时候遇到如下错误:'Microsoft.Jet.OLEDB.4.0'提供在本地没有注册 服务器是WIN2008 导致这个错误主要有几个原因: 1,服务器上没有装MS Excel中的 ...
- Python,OpenGL生命游戏
初学Python和OpenGL,练手的第一个小程序life.py,这个小程序在日后会不断调整,增加类.优化判断及操作 执行效果: 按正规生命游戏的规则: 1.周围生命等于3时产生生命 2.周围生命等于 ...
- DNS_PROBE_FINISHED_NXDOMAIN
DNS_PROBE_FINISHED_NXDOMAIN 用如下链接清除dns即可 chrome://net-internals/#dns dns不稳定 手动绑定host即可
- ubuntu下安装stm32开发环境
在windowns下开发stm32刚开始学最烦的就是创建工程模板,都不知道为什么要那样设置,而且步骤繁多.现在我告诉大家一个好消息,在linux下配置stm32开发环境包括创建工程,使用JLink仿真 ...
- python学习——练习题(13)
""" 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个" ...
- 转载----开发运维资产管理系统cmdb一些观点扯淡
在新公司负责全网的自动化运维平台及给各个业务线提供接口数据.这工作和以前做的很类似,也算是比较顺手的工作,这段时候遇见一些问题,导致开发的前进速度的放慢了,具体有哪些的不完善,我这里就先不摆出了,但是 ...
- oracle对表常用的操作
最近业务需要给表添加索引,因为数据量很大时,查询效率很低:老大建议使用索引: 之前总结的时候将索引没有记录,当然啦,也怪笔者基础薄弱,不管了,慢慢进步嘛,好了进入正题吧! 首先准备工作,先建两个临时表 ...
- 读取resource下文件
ArrayList<PatrolOper> patrolOpers = new ArrayList<>(); String jsonData = null; File json ...
- LInux命令执行http请求
Linux下提供了一些命令可以直接执行http请求,下面举例来介绍几个命令. http // GET请求 http http://127.0.0.1:2379/version http GET htt ...