Satyam
Satyam

Reputation: 15894

How to create custom UITableViewCell without using XIB

I found so many tutorials on how to create custom table view cell using XIB, but is it possible to create custom table view cell without using XIB? Can some one help me?

Upvotes: 9

Views: 8393

Answers (1)

Anand Gautam
Anand Gautam

Reputation: 2579

Yeah, You can create Custom table view cell without using a XIB.

For this, you have to create one new class in Xcode with the subclass of UITableViewCell. here you can't select any XIB options.

Now open your custom UITableViewCell class and here's code that will achieve what you want:

#import "CustomCell.h"

@implementation CustomCell

@synthesize ivUser;
@synthesize lblUserName;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:
(NSString *)reuseIdentifier

{

   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
           // Initialization code

        ivUser = [[UIImageView alloc] initWithFrame:CGRectMake(4.0f, 3.0f, 39.0f,
        38.0f)];
        lblUserName = [[UILabel alloc] initWithFrame:CGRectMake(58.0f, 8.0f, 50.0f,
        27.0f)];

        [self.contentView addSubview:ivUser];
        [self.contentView addSubview:lblUserName];

     }

   return self;
}

Now set your subviews coordinates according to the requirement, then use this cell in your CellForRowAtIndexPath and it should work.

Upvotes: 5

Related Questions