alexmngn
alexmngn

Reputation: 9587

UITableViewCell with Checkmark not visible

I've a problem with my UITableView inside PopoverController. When I touch cell, the didSelectRowAtIndexPath function is called, and the cell accessoryType is changed. Example simplified :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.listItems objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.tableView reloadData];
    [self.popoverController dismissPopoverAnimated:YES];
}

It's working, the cell are checked, but it's not visible on my tableview : I can't see the blue checkmark. However, in touch state on the cell, the checkmark is visible in white (and the cell background is gray). But not visible in default state.

Do you have any idea why my checkmark are not visible in default state ?

Thanks,

Edit: Add screenshot, for a cell with accessoryType = UITableViewCellAccessoryCheckmark

enter image description here

Upvotes: 6

Views: 6043

Answers (8)

Gaurav Sharma
Gaurav Sharma

Reputation: 755

Mine was the most stupidest reason. I had created a tableview in storyboard, with a View Controller of size 5.5 inch and forgot to apply the layout constraints.

Then I launched in a 4 inch phone, Everything looked fine except the accessory view was not visible because of tableviews width was greater than that of the phone screen. It took me 3 hours to find out my mistake.

Upvotes: 2

Gank
Gank

Reputation: 4667

-(UIImageView *)checkmarkImg{
    UIImage *image = [[UIImage imageNamed:@"ic_check_black_24dp.png"] changeColor:CLR_BUY];
    UIImageView *checkmark = [[UIImageView alloc] initWithImage:image];
    return  checkmark;
}

        cell.accessoryView = [self checkmarkImg];

Upvotes: 0

Iron Mike
Iron Mike

Reputation: 335

This happened to me when I changed the global tint color to white. Once I realized, I went into the UITableView and change the tint color locally for just this table. Fixed.

Upvotes: 12

Banker Mittal
Banker Mittal

Reputation: 1918

If you want to reload Data then you should store selected Id in some variable for single selection like rowIndex and then in

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //check index
    if (rowIndex==indexPath.row)
        {cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
}

Thanks.

Upvotes: 0

Mike
Mike

Reputation: 3084

Running under iOS 6.1, the behavior I see is a cell with a white check mark on an almost white background. This appears to be happening because the code that draws the check mark accessory believes the cell is in a highlighted state, so rather than drawing the check mark in the normal blue color, it is drawn in white.

Setting the selected state of the cell did not work for me but setting the highlighted state immediately before setting the accessory type did. With the following in place, I always get a dark blue check mark.

cell.highlighted = NO;
if (checked)
    self.accessoryType = UITableViewCellAccessoryCheckmark;
else
    self.accessoryType = UITableViewCellAccessoryNone;

Upvotes: 0

AndreyMan
AndreyMan

Reputation: 113

I've tried the answer Jacky Boy - didn't help. But something was there in the deselection...

So I've tried to deselect the cell in the didSelectRowAtIndexPath: before adding the checkmark accessory:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   [tableView deselectRowAtIndexPath:indexPath animated:YES];
   UITableViewCell* selectedCell = [tableView cellForRowAtIndexPath:indexPath];

   if (row != _selectedRow) {

       if (selectedCell.accessoryType == UITableViewCellAccessoryNone) {

          selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
          _selectedRow = row;

       } else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {

          selectedCell.accessoryType = UITableViewCellAccessoryNone;
       }
       [tableView reloadData];
   }

}

And for me it worked at last - the nice dark checkmark now is clearly visible on the cell!

Of course there is a part in cellForRowAtIndexPath: similar to described in arexx's answer.

Upvotes: 3

arexx
arexx

Reputation: 1743

I had a similar problem where, after reloading the row with a checkmark set as the accessory, the checkmark wouldn't be visible (but would be visible in white when the row was selected). In testing around the problem I discovered that the checkmark is always present in the cell, it's just white-on-white.

My understanding of the problem is that when I ask for the cell to be reloaded (so that I can show it with a checkmark), the existing cell is put on the reuse queue, but is at that time in a selected state (because the user just selected it). It's still in a selected state when the cell comes back off the reuse queue and you re-configure it in tableView:cellForRowAtIndexPath, and because it's selected, the accessory is set in white instead of in a visible colour.

To fix this, I added a line in tableView:cellForRowAtIndexPath to force the cell not to be selected. The accessory is now always visible when the new cell is displayed.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get a reusable cell - this only works with Storyboard prototype cells 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    // THE MAGIC BIT
    // Force the cell to not be in a selected state.    
    cell.selected = NO;
    // END MAGIC BIT

    if (indexPathIsPathOfSelectedRow) {
         // This is the selected cell, so show the checkmark accessory.
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
         // In case we're reusing a cell that previously showed a checkmark, clear it.
         cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

Upvotes: 2

Rui Peres
Rui Peres

Reputation: 25907

You are reloading the UITableView so in theory the cells are recreated and this time without the checkmark. Do the following:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.listItems objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.popoverController dismissPopoverAnimated:YES];
}

Upvotes: 0

Related Questions