Reputation: 14557
I am trying to use a custom image for the UIAccessoryView, however I can't seem to get it to work. When the table view launches, it doesn't update with any of the images. My code is the following:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
MenuItem *menuItem = [self.menuItems objectAtIndex:indexPath.row];
if (!tableViewCell)
{
tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
tableViewCell.textLabel.font = [UIFont fontWithName: CaptionDistractionBodyFontName size: 15];
imageView.contentMode = UIViewContentModeCenter;
tableViewCell.accessoryView = [[UIImageView alloc] initWithImage:nil];
}
tableViewCell.textLabel.text = menuItem.name;
UIImageView *imageView = (UIImageView*)tableViewCell.accessoryView;
imageView.image = nil;
if (menuItem.type == MenuItemTypeCheckMark)
{
if (menuItem.isCheckMarked)
{
imageView.image = [UIImage imageNamed:@"DisclosureViewX"];
}
}
else if (menuItem.type == MenuItemTypeSubmenu)
{
imageView.image = [UIImage imageNamed:@"DisclosureViewArrow"];
}
return tableViewCell;
}
I've tried a lot of different stuff, i.e. calling setNeedsLayout
, moving the code to a custom UITableViewCell
in layoutSubviews
and nothing seems to work. Aside from just creating a whole new accessory view myself, what is the correct way to go about this?
Upvotes: 1
Views: 1156
Reputation: 3041
I think your problem is that image view creates with zero size. Use initWithFrame: or initWithImage:, where image is not nil. After created with initWithImage:, image view will have bounds set to image size. A cell will center the accessory view automatically.
Upvotes: 0
Reputation: 318854
Since you initially setup the UIImageView
with a nil
image, the image view's frame has a zero width and height. You need to reset the frame after assigning an actual image.
imageView.image = [UIImage imageNamed:@"DisclosureViewX"];
imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
Edit:
Actually it would be even easier to call [imageView sizeToFit]
after setting the image instead of setting the frame
.
Upvotes: 2
Reputation: 763
try doing
tableViewCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DisclosureViewX"]];
Upvotes: 1