UITableViewCell Accessory Type Checked on Tap & Set other unchecked

后端 未结 9 2247
小蘑菇
小蘑菇 2020-12-03 03:02

I am confused a little bit about settings table view cell accessories.

I have fixed two sections in my table

  • Home
  • Office

What I

相关标签:
9条回答
  • 2020-12-03 03:36
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    
        if (newCell.accessoryType == UITableViewCellAccessoryNone) {
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else {
            newCell.accessoryType = UITableViewCellAccessoryNone;
        }
    
    
    }
    

    also you need to remove the checkmark accessory on cellForRowAtIndexPath

    if ([selectedOptionsArray indexOfObject:cell.textLabel.text] != NSNotFound) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    0 讨论(0)
  • 2020-12-03 03:39

    A shortest and easiest way to implement logic for checkmark in selected row only.....


    1.Create a global object of NSIndexPath..

    selectedIndexPathForCheckMark= [[NSIndexPath alloc] init];
    

    2.In didSelectRowAtIndexPath..

    //Assiging selected indexPath to the declared object

    selectedIndexPathForCheckMark = indexPath;  
    [tableView reloadData];
    

    3.in cellForRowAtIndexPath..

    if ([selectedIndexPathForCheckMark isEqual:indexPath]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else {
        //setAccessoryType None if not get the selected indexPath
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    
    0 讨论(0)
  • 2020-12-03 03:39

    Considering you only want to maintain a single selection, and if you have a custom UITableViewCell, I would recommend the following.

    In your UITableViewController

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        //... 
        let data = dataArray[indexPath.row]
        if data.isSelected {
            tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)
        }
        //...
    
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let data = dataArray[indexPath.row]
        data.isSelected = true
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        cell?.setSelected(true, animated: true)
    }
    
    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        let data = dataArray[indexPath.row]
        data.isSelected = false
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        cell?.setSelected(false, animated: true)
    }
    

    In the custom UITableViewCell

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
        // Configure the view for the selected state
        self.accessoryType = .None
        if selected {
            self.accessoryType = .Checkmark
        }
    }
    

    As long as allowsMultipleSelection is not enabled, it will automatically handle deselecting other cells when a new one is selected. If allowMultipleSelection is enabled, then it will work as well, except you will just need to tap again to deselect.

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