views:

70

answers:

1

I have a table cell to which I'm adding subviews programmatically. All the textual subviews work fine, but I can't get an image subview working at all.

You'll notice that I set the background color to black. This is simply to indicate to me that the subview is indeed being initialized and positioned properly within the cell. When I remove the background color there is nothing there.

Also, the cell style is UITableViewCellStyleDefault but I don't think that's pertinent for custom subviews. I want the image positioned on the right, which is why I'm not using the standard imageView property that cells offer.

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        // ... add textual views ...

        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"clock.png"]];
        img.frame = CGRectMake(271.0f, 10.0f, 19.0f, 22.0f);
        img.backgroundColor = [UIColor blackColor];

        [cell addSubview:img];
    }

    // ... more code ...

    return cell;
}
A: 

Your code looks fine to an extent. One issue I had when I was creating my own UITableViewCells in code was I was creating the cells with variables like you are but you shouldn't do this.

What you should do is create the cell and then set the variables. If cells are different you should use different reuse identifiers. Hope this makes sense if not let me know and I'll update.

Rudiger
I'm not sure to which variables you are referring; there are several in my example alone. :) Can you specify exactly which ones?
thebossman
The image. Every cell you are going to return has an clock image in it
Rudiger