audience
audience

Reputation: 2412

Checkmark won't show in TableViewCell on iOS7

I'm working on a weird issue right now. My Apps Deployment Target is set to iOS6, so I want to support both iOS6 and iOS7.

I just have a simple UITableView, in which the user can select the preferred notification sound.

The code for - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CheckmarkCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        [cell setTintColor:[UIColor redColor]];
        if (indexPath.section == 0){
            cell.textLabel.text = [_availableSounds objectAtIndex:indexPath.row];
            if (indexPath.row == _checkedSoundIndexPath.row) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
        else {
// Unrelated, another settings cell
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            return cell;
        }

My - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath looks like the following:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 0) {
        return;
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    [[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
    if (_checkedSoundIndexPath != indexPath) {
        [[self.tableView cellForRowAtIndexPath:_checkedSoundIndexPath] setAccessoryType:UITableViewCellAccessoryNone];
    }
    _checkedSoundIndexPath = indexPath;
}

The problem is that an iOS7 iPhone won't show the checkmark as expected. Running the same code on an iOS6 iPhone works as expected. I tried to insert [cell setTintColor:[UIColor redColor]]; but without any luck. Even if I remove all AccessoryType related code and add the checkmark in my storyboard nothing appears. See screenshots below (first is iOS6 and second is iOS5).

Does anyone have an idea ? Or is it a bug in iOS7 ?

Thanks in advance !

Edit:

Even if I make a new simple UITableViewController, with just 5 cells with the Accessory set to UITableViewAccessoryTypeCheckmark, the Checkmarks won't appear on iOS7.

iOS6 on an iPhone 4 iOS7 on an iPhone 5

Upvotes: 6

Views: 4418

Answers (8)

simple_code
simple_code

Reputation: 767

I know the original question uses a simple UITableViewCell but this might work for someone else with a custom table view cell.

I have a custom UITableViewCell subclass. I tried everything but the checkmark won't show. Eventually I realised that I overrode layoutSubviews but forgot to call [super layoutSubviews]. Which meant the checkmark would not show correctly.

 - (void)layoutSubviews
{
    [super layoutSubviews];
    //...custom layout here
}

Upvotes: 0

Ashok
Ashok

Reputation: 6244

I would rather reload tableview (preferably only the affected rows - using reloadRowsAtIndexPaths) after settings appropriate data in didSelectRowAtIndexPath: method.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Other logic..

            [tableView deselectRowAtIndexPath:indexPath animated:NO]; // Would like to look for an alternative method to avoid this refresh??

            NSMutableArray *reloadArray = [NSMutableArray arrayWithCapacity:2];
            [reloadArray addObject:indexPath];

            [reloadArray addObject:_checkedSoundIndexPath];

            self.checkedSoundIndexPath = indexPath;   // Should set this before reload
            [tableView reloadRowsAtIndexPaths:reloadArray withRowAnimation:UITableViewRowAnimationAutomatic];
}

And, most importantly, add these lines in cellForRowAtIndexPath: method -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // All your other code for this section followed by these lines...

    if([_checkedSoundIndexPath compare:indexPath] == NSOrderedSame) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Other sections code
    return cell;
}

Upvotes: 0

R Rungsikavanich
R Rungsikavanich

Reputation: 173

I can solve this problem by set tint color for Cell It didnt show because cell tint in white ,and cell selectionstyle is none

cell.tintColor = [UIColor blackColor];

    if(cell.selected){
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];

    }

Upvotes: 0

Dev Patel
Dev Patel

Reputation: 290

In my application it working perfect,check it 
//take it in .h file mutable arSelectedRows;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
   [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

   //Do anything you want for cell here
   if([arSelectedRows containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
   else {
          cell.accessoryType = UITableViewCellAccessoryNone;
    }
   return cell;
}

 #pragma mark - Table view delegate

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   if(cell.accessoryType == UITableViewCellAccessoryNone) {
       cell.accessoryType = UITableViewCellAccessoryCheckmark;
       [arSelectedRows addObject:indexPath];
   }
   else {
      cell.accessoryType = UITableViewCellAccessoryNone;
       [arSelectedRows removeObject:indexPath];
  }
   NSLog(@"id are here :%@",arSelectedIDs);

   [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

may be ,it will helpful.

Upvotes: 2

off
off

Reputation: 97

I had the exact same problem as you are for a long time. In my case I implemented some appearance tweaks. And one of them was

[[UIView appearance] setTintColor:[UIColor whiteColor]];

Try to find in your project global things like that.

Upvotes: 4

Techie
Techie

Reputation: 143

I think the problem is with your checksoundIndexpath, please check whether it has a correct indexpath, Or First check with hardcoded indexpath.

you are not initializing the checksoundindxpath.

Also I noticed you are not assigning the selected row indexpath to _checkSoundIndexpath, Only you are checking if both indexpaths(current indexpaths and _checksoundindexpath) are equal, But if they are different then you should assign the selected indedxpath to _checksound indexpath.

Upvotes: 0

Alvaro Rojas
Alvaro Rojas

Reputation: 562

I had a similar problem and I solved this issues changing the tint color of the uitableview

I changed the tintcolot of uitable by InterfaceBuilder to Default color

or

tableView.tintColor =  [UIColor blackColor];

Upvotes: 10

Toseef Khilji
Toseef Khilji

Reputation: 17409

No, There is No Problem with UITableViewCellAccessoryCheckmark in iOS 7,Make sure you have implemented correct logic for it.

Upvotes: 0

Related Questions