Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 281
Satyam Avatar asked Feb 26 '11 14:02

Satyam


1 Answers

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.

like image 102
Anand Gautam Avatar answered Oct 19 '22 23:10

Anand Gautam