Only one UITableViewCellAccessoryCheckmark allowed at a time

前端 未结 2 1300
灰色年华
灰色年华 2021-01-06 17:42

I have a tableview with a section that contains a list of sounds the user can \"pick.\" Only the currently selected sound should show a UITableViewCellAccessoryCheckma

相关标签:
2条回答
  • 2021-01-06 18:04

    You aren't remembering to reset the accessory type for dequeued cells.

    This should fix it for you;

      cell.accessoryType = UITableViewCellAccessoryNone;
    
      if (indexPath.row == index) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
      }
    

    Handy tip : You may find it useful to use switch(indexPath.section) rather than lots of if ... elseif ... else constructs. Although you're OK now with just 2 sections, I find that using switch makes it easier to scale up the number of sections and also makes it mentally easier to read throught the code since you often have if .. else constructs used for other things - having the different syntax helps make it clearer what the tests are for.

    0 讨论(0)
  • 2021-01-06 18:20

    In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathafter each declaration of cell try reseting the cells accessoryType by adding following line: cell.accessoryType = UITableViewCellAccessoryNone;

    0 讨论(0)
提交回复
热议问题