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 = (UITableV开发者_如何学PythoniewCell *)[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;
}
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.
精彩评论