Maksim Pecherskiy
13 November 2012

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!!!

Grab the code!

除了上文提到的,还可以这样

-(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的更多相关文章

  1. Collection View Programming Guide for iOS---(六)---Creating Custom Layouts

    Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...

  2. 【IOS笔记】Creating Custom Content View Controllers

    Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...

  3. 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 ...

  4. 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 ...

  5. View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers

    Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...

  6. [TensorFlow] Creating Custom Estimators in TensorFlow

    Welcome to Part 3 of a blog series that introduces TensorFlow Datasets and Estimators. Part 1 focuse ...

  7. 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 ...

  8. Creating Custom Login Screen In Oracle Forms 10g

    Below is the example plsql unit to validate login credentials and after successful validation open a ...

  9. 管理后台-第一部分:Creating custom sections in Umbraco 7 - Part 1(翻译文档)

    在Umbraco上每个部分都可以被称为一个应用程序,所以这些部分和应用程序基本上是一样的.我们首先要做的事情是需要创建应用程序.在这个例子中,我不会去摆弄xml文件或是数据库——我将使用类来创建我的内 ...

随机推荐

  1. Hibernate 一对一、一对多、多对多注解cascade属性的总结

    作用:是否级联被注解字段里面的对象.可选值:javax.persistence.CascadeType.PERSIST, MERGE, REMOVE, REFRESH, DETACH, ALL.可选其 ...

  2. java类继承总结一 父类类型与子类类型之间的转化问题(转)

    java类继承总结一 父类类型与子类类型之间的转化问题 本文将通过一个实例描述父类类型与子类类型之间的转化问题,这个很特殊的问题常常会导致一些潜在的危险,让你整整一个晚上都在调试程序以解决一个让人抓狂 ...

  3. News Master-DC and Marvel they are super heroes mother

    News Master Good evening everyone,I’m Jason,I’m glad to be news master to share something, Tonight I ...

  4. Centos下Apache+Tomcat集群--搭建记录

    一.目的 利用apache的mod_jk模块,实现tomcat集群服务器的负载均衡以及会话复制,这里用到了<Cluster>. 二.环境 1.基础:3台主机,系统Centos6.5,4G内 ...

  5. 第二章:Android Studio概述(二)[学习Android Studio汉化教程]

    The Main Menu Bar 主菜单栏  主菜单栏位于Android Studio的最上面,你几乎可以利用主菜单和其子菜单来执行任何操作.不像Android Studio中其他的一些菜单,主菜单 ...

  6. 第十章 消息驱动的微服务: Spring Cloud Stream

    Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的框架. 它可以基于Spring Boot 来创建独立的. 可用于生产的 Spring 应用程序. 它通过使用 Sprin ...

  7. JVM常用参数设置

    堆内存设置 示例: java -Xmx4550m -Xms4550m -Xss128k -XX:NewRatio=5 -XX:SurvivorRatio=5 -Xmx4550m:设置JVM最大可用内存 ...

  8. 归纳一下:C#线程同步的几种方法

    转自原文 归纳一下:C#线程同步的几种方法 我们在编程的时候,有时会使用多线程来解决问题,比如你的程序需要在后台处理一大堆数据,但还要使用户界面处于可操作状态:或者你的程序需要访问一些外部资源如数据库 ...

  9. 关于LOH(Large Object Heap)及内存泄漏

    关于LOH(Large Object Heap)的. .NET CLR中对于大于85000字节的内存既不像引用类型那样分配到普通堆上,也不像值类型那样分配到栈上,而是分配到了一个特殊的称为LOH的内部 ...

  10. <转>linux操作系统编程——共享内存读写(采用信号量进行同步互斥)

    http://blog.csdn.net/yanghaoran321/article/details/7872722 程序要求: 创建一个写端和一个读端,写端写入数据后读端才开始读,读端读完数据后,写 ...