Tuesday, April 26, 2011

IPhone: Custom Table Cell

In order to make a custom table cell you have to handle the cell contentView:

- (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];
    }
   
    // Set up the cell...
    pinFreeNumberItem *cellValue = [tableData objectAtIndex:indexPath.row];
   
    UIView *myContentView = cell.contentView;
    UILabel* topLeftLabel = [[UILabel alloc] initWithFrame: CGRectMake(2, -6, 350, 30)];
    topLeftLabel.text = @"hi1";
   
    [myContentView addSubview:topLeftLabel];
   
    UILabel* bottomRightLabel = [[UILabel alloc] initWithFrame: CGRectMake(170, 24, 350, 15)];
    bottomRightLabel.text =@"hi2";
   
    [myContentView addSubview:bottomRightLabel];
   
    UILabel* bottomLeftLabel = [[UILabel alloc] initWithFrame: CGRectMake(2, 24, 150, 15)];
    bottomLeftLabel.text = @"hi3";
   
    [myContentView addSubview:bottomLeftLabel];
   
    return cell;
}

Resources:
http://stackoverflow.com/questions/5337340/custom-uitablecell
http://iphone.zcentric.com/2008/08/05/custom-uitableviewcell/

No comments: