I’m using my own UITableViewCells – instead of creating some in IBuilder, because then you have to load them each time from NIB file and it really slowdown your app. So, this is simple example how to do it – TableCell contains only two non-editable labels with text.
Start from you XCode and add new Class – UITableViewCell, enter some cell name and.. do code :)
// MyIndexTableViewCell.h // // Created by Tom Meinlschmidt on 5.4.2010. // Copyright 2010 Tom Meinlschmidt. All rights reserved. // #import@interface MyIndexTableViewCell : UITableViewCell { UILabel *cellTitle; UILabel *cellDescription; } @property (nonatomic, retain) UILabel *cellTitle; @property (nonatomic, retain) UILabel *cellDescription; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier bgcolor:(UIColor*) bgColor; -(void) setCellBackgroundColor:(UIColor *) bgColor; @end
// MyIndexTableViewCell.m // // Created by Tom Meinlschmidt on 5.4.2010. // Copyright 2010 Tom Meinlschmidt. All rights reserved. // #import "MyIndexTableViewCell.h" @implementation MyIndexTableViewCell @synthesize cellTitle; @synthesize cellDescription; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { return [self initWithStyle:style reuseIdentifier:reuseIdentifier bgcolor:[UIColor whiteColor]]; } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier bgcolor:(UIColor*) bgColor{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // Initialization code // we need a view to place our labels on. UIView *myContentView = self.contentView; myContentView.backgroundColor = bgColor; // description self.cellDescription = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 260, 21)]; self.cellDescription.backgroundColor = [UIColor clearColor]; self.cellDescription.opaque = NO; self.cellDescription.textColor = [UIColor grayColor]; self.cellDescription.highlightedTextColor = [UIColor whiteColor]; self.cellDescription.font = [UIFont fontWithName:@"Helvetica-Oblique" size:10.0f]; self.cellDescription.textAlignment = UITextAlignmentLeft; // default [myContentView addSubview:self.cellDescription]; [self.cellDescription release]; // title self.cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 260, 21)]; self.cellTitle.backgroundColor = [UIColor clearColor]; self.cellTitle.opaque = NO; self.cellTitle.textColor = [UIColor blackColor]; self.cellTitle.highlightedTextColor = [UIColor whiteColor]; self.cellTitle.font = [UIFont fontWithName:@"Helvetica" size:17.0f]; self.cellTitle.textAlignment = UITextAlignmentLeft; // default [myContentView addSubview:self.cellTitle]; [self.cellTitle release]; } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; } -(void) setCellBackgroundColor:(UIColor *) bgColor { self.contentView.backgroundColor = bgColor; } - (void)dealloc { [cellTitle release]; [cellDescription release]; [super dealloc]; } @end
and use it in TableViewController class as:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // init UITableViewCell *cell = nil; NSUInteger row_index = [indexPath row]; SelectTableRowEntity *row = [selectData.selectFrom objectAtIndex:row_index]; cell = (MyIndexTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:@"MyIndexTableViewCell"]; if (cell==nil) { cell = [[MyIndexTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIndexTableViewCell"]; } [[(MyIndexTableViewCell*)cell cellTitle] setText:row.rowName]; [[(MyIndexTableViewCell*)cell cellDescription] setText:row.rowDescription]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (selectData.value==row.rowValue) { [(MyIndexTableViewCell*)cell setCellBackgroundColor:[TableSection resultGreen]]; } else { // Set up the cell... [(MyIndexTableViewCell*)cell setCellBackgroundColor:[UIColor whiteColor] ]; } return cell; }