Code: TableView Cell’s with less code

By December 26, 2013 May 25th, 2020 No Comments

A common app usually use a UITableView, but sometimes when you want to set up a custom UITableViewCell the result is huge amount of unreadable code.

For this reason we propose to use this way:


Configure your xib with your own design.


#import <UIKit/UIKit.h>
@class CODCustomModel;

@interface CODCustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *subtitle;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UILabel *date;

//Instance Method
- (id)initWithOwner:(id)owner;
- (void)configureView;
- (void)setData:(CODCustomModel*)aData;
//Class Method
+ (NSString *)reuseIdentifier;



#import "CODCustomCell.h"
#import "CODCustomModel.h"
@implementation CODCustomCell

+ (UINib*)nib
        // singleton implementation to get a UINib object
    static dispatch_once_t pred = 0;
    __strong static UINib* _sharedNibObject = nil;
    dispatch_once(&pred, ^{
        _sharedNibObject = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
    return _sharedNibObject;

- (NSString *)reuseIdentifier
    return [[self class] reuseIdentifier];

+ (NSString *)reuseIdentifier
        // return any identifier you like, in this case the class name
    return NSStringFromClass([self class]);


  //configure any subview

- (id)initWithOwner:(id)owner
    return [[[[self class] nib] instantiateWithOwner:owner options:nil] objectAtIndex:0];

- (void)setData:(CODCustomModel*)aData{

   self.title.text = aData.title;
   self.subtitle.text = aData.subtitle;
   //.. rest of ui


and now in your table view controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Lazy instantiation 
    CODCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:[CODCustomCell reuseIdentifier]];
    if (cell == nil) {
        cell = [[CODCustomCell alloc] initWithOwner:self];
        [cell configureView];
    //Get the data
    CODCutomModel * item = dataSource[indexPath.row];
    [cell setData:item];
    return cell;


As you see, its so clean an you will get much readable code for you and your team.