user3258468
user3258468

Reputation: 354

Custom TableViewCell programmatically

I have source code that does not use storyboard or xib. It contains a UITableView and the cells use the standard cell.textlabel.

What i want is to use a custom TableViewCell, normally i would create a UITableViewCell class and connect them in storyboard, but i cant connect them here since its not using storyboard or xib.

I have following UITableViewClass

@interface chatCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UIImageView *homeTeamImage;
@property(nonatomic, weak) IBOutlet UILabel *homeTeamLabel;
@property(nonatomic, weak) IBOutlet UIImageView *awayTeamImage;
@property(nonatomic, weak) IBOutlet UILabel *awayTeamLabel;
@end

How can i place them inside the TableViewCell and connect them to the properties?

so i can start using

cell.homeTeamImage
cell.homeTeamLabel
cell.awayTeamImage
cell.awayTeamLabel

Upvotes: 0

Views: 1055

Answers (4)

Karthik murugasan
Karthik murugasan

Reputation: 1

@interface chatCell : UITableViewCell
{
    UIImageView *homeTeamImage;
    UILabel *homeTeamLabel;
    UIImageView *awayTeamImage;
    UILabel *awayTeamLabel;
}

@property(nonatomic, weak) UIImageView *homeTeamImage;
@property(nonatomic, weak) UILabel *homeTeamLabel;
@property(nonatomic, weak) UIImageView *awayTeamImage;
@property(nonatomic, weak) UILabel *awayTeamLabel;

@end


and don't forget to

    enter code here

@synthesize "All properties"

Upvotes: 0

vokilam
vokilam

Reputation: 10313

See Programmatically Adding Subviews to a Cell’s Content View from Table View Programming Guide for iOS. In this example cells are created in tableView:cellForRowAtIndexPath: but you can use same approach (add views on cell's content view) in UITableViewCell subclass.

Upvotes: 0

drolando
drolando

Reputation: 507

You can override the - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath. Instead of returning a standard UITableViewCell you can return yours

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    chatCell c = [[chatCell alloc] init];
    return c;
}

You don't need the IBOutlet keyword: it isn't a type, it's only used by xcode when you use the storyboard. I think that without storyboard you should change the properties to strong pointers because no one else is strongly pointing to them so they'll be deleted.

The @synthesize is not mandatory if you use xcode 5. It's required only if you override both setter and getter of a property.

Upvotes: 0

Atomix
Atomix

Reputation: 13842

You don't need to use IBOutlet, since you don't want to use the properties in IB

@interface chatCell : UITableViewCell
{
    UIImageView *homeTeamImage;
    UILabel *homeTeamLabel;
    UIImageView *awayTeamImage;
    UILabel *awayTeamLabel;
}

@property(nonatomic, weak) UIImageView *homeTeamImage;
@property(nonatomic, weak) UILabel *homeTeamLabel;
@property(nonatomic, weak) UIImageView *awayTeamImage;
@property(nonatomic, weak) UILabel *awayTeamLabel;

@end

and don't forget to

@synthesize homeTeamImage, ...; 

in your implementation file.

Upvotes: 1

Related Questions