Search code examples
iosuitableviewuiimage

How do I center an image in a UITableViewCell?


I use this code to create an image inside a cell:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"] autorelease];


    switch (indexPath.row) {
    ...
    case 5:
            cell.imageView.image = [UIImage imageNamed:@"Search.png"];
            break;
        default:
            break;
    }
    return cell;
}

How do I center the image in the cell? Do I HAVE to subclass UITableViewCell?


Solution

  • Better and efficient way to do this is using Custom UITableViewCell. So in feature, you can easily customize the TableViewCell.

    You can do this through both code and Interface builder.

    Custom UITableViewCell Using Interface Builder

    Customize Table View Cells for UITableView