Creating Custom UITableViewCells with NIB files

简介:
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

Select View

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

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

Drag IBACTION

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;}
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!
















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/p/3450360.html ,如需转载请自行联系原作者

相关文章
|
4月前
|
JavaScript
解决报错did you register the component correctly? For recursive components, make sure to provide the “na
解决报错did you register the component correctly? For recursive components, make sure to provide the “na
Get Files In Folder [ DTFolderFiles ] Plug-in description
Get Files In Folder [ DTFolderFiles ] Plug-in description
42 0
error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
210 0
error while loading shared libraries: libxx.so: cannot open shared object file: No such file
error while loading shared libraries: libxx.so: cannot open shared object file: No such file
100 0
strange behavior:why u31000 is accessed for Extension project
Created by Wang, Jerry, last modified on May 20, 2015
105 0
strange behavior:why u31000 is accessed for Extension project
why our extension project didn't load S3 view and controller
Created by Wang, Jerry, last modified on May 20, 2015
100 0
why our extension project didn't load S3 view and controller
|
前端开发 Java Maven
reuse project css less path folder hierarchy issue
reuse project css less path folder hierarchy issue
139 0
reuse project css less path folder hierarchy issue
|
Unix Shell Linux