UITableViewCell

Custom Cells

 * http://github.com/sburlot/tableviewcellwithbkgnd

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];   }    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;    UILabel *label = [[[UILabel alloc] init] autorelease];    label.font = [UIFont boldSystemFontOfSize:16.0f];    label.frame = CGRectMake(40.0f, 10.0f, 220.0f, 22.0f);    label.textColor = [UIColor whiteColor];    label.backgroundColor = [UIColor clearColor];    label.opaque = NO;    NSArray *list = [NSArray arrayWithObjects:@"First", @"Second", @"Third", nil];    label.text = [list objectAtIndex:indexPath.row];    [cell.contentView addSubview:label];    CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero];    if (indexPath.row == 0) {        bgView.position = CustomCellBackgroundViewPositionTop;    } else if (indexPath.row == 1) {        bgView.position = CustomCellBackgroundViewPositionMiddle;    } else {        bgView.position = CustomCellBackgroundViewPositionBottom;    }    cell.backgroundView = bgView;    cell.accessoryView = ;    return cell; }
 * http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/

-(void)drawRect:(CGRect)rect {   // Drawing code
 * http://stackoverflow.com/questions/400965/how-to-customize-the-background-border-colors-of-a-grouped-table-view

CGContextRef c = UIGraphicsGetCurrentContext; CGContextSetFillColorWithColor(c, [fillColor CGColor]); CGContextSetStrokeColorWithColor(c, [borderColor CGColor]); CGContextSetLineWidth(c, 2);

if (position == CustomCellBackgroundViewPositionTop) {

CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ; CGFloat miny = CGRectGetMinY(rect), maxy = CGRectGetMaxY(rect) ; minx = minx + 1; miny = miny + 1;

maxx = maxx - 1; maxy = maxy ;

CGContextMoveToPoint(c, minx, maxy); CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE); CGContextAddArcToPoint(c, maxx, miny, maxx, maxy, ROUND_SIZE); CGContextAddLineToPoint(c, maxx, maxy);

// Close the path CGContextClosePath(c); // Fill & stroke the path CGContextDrawPath(c, kCGPathFillStroke); return; } else if (position == CustomCellBackgroundViewPositionBottom) {

CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ; CGFloat miny = CGRectGetMinY(rect), maxy = CGRectGetMaxY(rect) ; minx = minx + 1; miny = miny ;

maxx = maxx - 1; maxy = maxy - 1;

CGContextMoveToPoint(c, minx, miny); CGContextAddArcToPoint(c, minx, maxy, midx, maxy, ROUND_SIZE); CGContextAddArcToPoint(c, maxx, maxy, maxx, miny, ROUND_SIZE); CGContextAddLineToPoint(c, maxx, miny); // Close the path CGContextClosePath(c); // Fill & stroke the path CGContextDrawPath(c, kCGPathFillStroke); return; } else if (position == CustomCellBackgroundViewPositionMiddle) { CGFloat minx = CGRectGetMinX(rect), maxx = CGRectGetMaxX(rect) ; CGFloat miny = CGRectGetMinY(rect), maxy = CGRectGetMaxY(rect) ; minx = minx + 1; miny = miny ;

maxx = maxx - 1; maxy = maxy ;

CGContextMoveToPoint(c, minx, miny); CGContextAddLineToPoint(c, maxx, miny); CGContextAddLineToPoint(c, maxx, maxy); CGContextAddLineToPoint(c, minx, maxy);

CGContextClosePath(c); // Fill & stroke the path CGContextDrawPath(c, kCGPathFillStroke); return; } else if (position == CustomCellBackgroundViewPositionSingle) { CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ; CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect) , maxy = CGRectGetMaxY(rect) ; minx = minx + 1; miny = miny + 1;

maxx = maxx - 1; maxy = maxy - 1;

CGContextMoveToPoint(c, minx, midy); CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE); CGContextAddArcToPoint(c, maxx, miny, maxx, midy, ROUND_SIZE); CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, ROUND_SIZE); CGContextAddArcToPoint(c, minx, maxy, minx, midy, ROUND_SIZE);

// Close the path CGContextClosePath(c); // Fill & stroke the path CGContextDrawPath(c, kCGPathFillStroke); return; } } One thing I ran into with the above CustomCellBackgroundView code from Mike Akers which might be useful to others:

cell.backgroundView doesn't get automatically redrawn when cells are reused, and changes to the backgroundView's position var don't affect reused cells. That means long tables will have incorrectly drawn cell.backgroundViews given their positions.

To fix this without having to create a new backgroundView every time a row is displayed, call [cell.backgroundView setNeedsDisplay] at the end of your -[UITableViewController tableView:cellForRowAtIndexPath:]. Or for a more reusable solution, override CustomCellBackgroundView's position setter to include a [self setNeedsDisplay].

Dynamic Height
"Create, configure, and add a UILabel as a subview of the contentView in the cell." "Calculate the height in the UITableView delegate method, – (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;" "Calculate the frame for the UILabel in the UITableView delegate method, – (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;"
 * http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/