简体   繁体   中英

ios/xcode:How to Add Accessory View to Custom Cell

I am following a tutorial that uses a custom cell created in Storyboard in a tableview and then adds an accessory view. The table is loading okay. However when I try to add an accessory view (an image), nothing appears. Could there be some setting in storyboard that is wrong? A problem in the code? Or why is the accessory view not appearing

Here is the method where the tutorial says to add the accessory view:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    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];
    }
//HERE IS ACCESSORY VIEW
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox.jpg"]];
    idTableCell.accessoryView = imageView;
//   [self updateAccessoryForTableCell:cell atIndexPath:indexPath];
          // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    return cell;
}

Thanks for any suggestions

static NSString *cellIdentifier = @"CELLIDENTIFIER";

UITableViewCell *customCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
UIImageView *sampleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sampleImage.png"]];
[customCell setAccessoryView:sampleImage];

Hope it useful for you.!!!

It should work. Please try blow code. there is no issue on Storyboard.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"customCell";
    CustomCellView *cell = [ctableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    long row = [indexPath row];
    cell.name.text = carname[row];
    cell.model.text = carModel[row];
    cell.carImg.image = [UIImage imageNamed:carImage[row]];

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

    // Configure the cell...

    return cell;
}

在此处输入图片说明

Change the dequeueReusableCellWithIdentifier: call to:

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

This will always returns a cell, so you will never have to check for nil.

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

I don't think any of the above answers have addressed your issue. You are right by mentioning that it all has to do with your storyboard.

In order to fix this, go into your Main.storyboard, click on the table view and reset the view to suggested constraints following the below picture. 重置为建议的约束

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