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. python做语音信号处理

    音频信号的读写.播放及录音 标准的python已经支持WAV格式的书写,而实时的声音输入输出需要安装pyAudio(http://people.csail.mit.edu/hubert/pyaudio ...

  2. 使用 Windows 运行时中异步性来始终保持应用程序能够快速流畅地运行

    转自:http://blogs.msdn.com/b/windowsappdev_cn/archive/2012/03/26/windows.aspx 人类的思维方式在本质上不是同步的,这直接影响着我 ...

  3. java传递是引用的拷贝,既不是引用本身,更不是对象

    java传递是引用的拷贝,既不是引用本身,更不是对象 2008-09-16 04:27:56|  分类: Java SE|举报|字号 订阅     下载LOFTER客户端     1. 简单类型是按值 ...

  4. oracle打补丁步骤简介

    1.了解opatchopatch是用于维护"个别"补丁的,有人称其为interim path或是one-off patch该命令的存放位置在$ORACLE_HOME下的OPatch ...

  5. sql server xml 截断

    c#读取 sql生成的xml时,发生阶段. 加,type 解决

  6. Python 模块 - jieba

    安装 jieba pip3 install jieba jieba 支持三种分词模式: 精确模式:将句子最精确地切开,适合文本分析 全模式:把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不 ...

  7. Deep Learning 阅读笔记:Convolutional Auto-Encoders 卷积神经网络的自编码表达

    需要搭建一个比较复杂的CNN网络,希望通过预训练来提高CNN的表现. 上网找了一下,关于CAE(Convolutional Auto-Encoders)的文章还真是少,勉强只能找到一篇瑞士的文章. S ...

  8. RabbitMQ_direct

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @version: @author: morgana @licens ...

  9. ArcGIS 二次开发增加工具栏按钮时,给按钮图标设置失败

    在类的构造函数中通过下面的代码获得的bitmap为null: string bitmapResourceName = GetType().Name + ".bmp"; base.m ...

  10. 小米手机Root 刷机

    需要备份的资料: miui系统资料:电话.短信.便签,有小米云账号wifi下自己会备份好的. 应用数据:微信.qq 聊天记录: UC浏览器收藏夹. root后, rootexplorer,选择某个文件 ...