1

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

Leo
  • 24,596
  • 11
  • 71
  • 92
  • you can make extra UIImage in place of Accesory view and apply the Image programatically..this is only alternative way – Rahul Jun 08 '15 at 12:39
  • There is also a class for the custom table cell. Do I have to put anything in that class to allow for accessory view? Tutorial does not mention it. –  Jun 09 '15 at 11:02

4 Answers4

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

0

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

Yalamandarao
  • 3,852
  • 3
  • 20
  • 27
  • This seems to be the same as my code but my code is not working. That's why I thought problem might lie in storyboard. Could it have anything to do with Accessory in Cell file inspector? I have it set to none. –  Jun 09 '15 at 02:54
  • You are saying the problem might be in storyboard? If so, what should I change? I already tried cutting and pasting code in above answer into my project but still is not working so don't think there is a typo in my code. –  Jun 09 '15 at 10:52
0

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];
// }
Bannings
  • 10,376
  • 7
  • 44
  • 54
0

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

Ahmed Ebaid
  • 417
  • 6
  • 21