简体   繁体   中英

ios/xcode/objective-c: How to create accessory view with image in custom cell?

My understanding is that to show an accessory view with an image you need to alter cellForRowWithIndex Path.

Based on various tutorials and questions in SO I've thrown everything but the kitchen sink into this method. Some say you only need to include the accessory view. Others say you need a button. Regardless, the image is still not displaying.

I've also been told the issue is not in storyboard as in one of the stock accessory views vs. none.

Can anyone recommend a sure-fire way to get the image to display in the accessory view:

Here is the method I have been trying without success:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   // UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    IDContactImport *contactImport = self.contacts[indexPath.row];
    IDTableViewCell *idTableCell =(IDTableViewCell *)cell;
    idTableCell.contactImport=contactImport;
    if (contactImport.imageData==nil)
    {
        idTableCell.iconView.image = [UIImage imageNamed:@"headshot.jpg"];
    }
    else{
       idTableCell.iconView.image = [UIImage imageWithData:contactImport.imageData];
    }
    UIImage *image = [UIImage imageNamed:@"checked.gif"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];

    [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;


    NSLog(@"Trying to load accessory image");

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.gif"]];
    cell.accessoryView = imageView;
  //  UIImage *image = [UIImage   imageNamed:@"headshot.jpg"];

 //   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 //   CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];

    [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
    return cell;
}

You have the correct answer in there. But it gets clobbered later on in the code.

This is correct:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.gif"]];
cell.accessoryView = imageView;

Also, use PNG images, not GIFs. It's a better format for iOS development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM