user4038028
user4038028

Reputation:

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

Upvotes: 1

Views: 5347

Answers (4)

Ahmed Ebaid
Ahmed Ebaid

Reputation: 417

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. Resetting to suggested constraints

Upvotes: 0

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.!!!

Upvotes: 2

Bannings
Bannings

Reputation: 10479

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];
// }

Upvotes: 0

Yalamandarao
Yalamandarao

Reputation: 3862

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;
}

enter image description here

Upvotes: 0

Related Questions