didDeselectRowAtIndexPath dont work

Hello friends


I have this code:


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> Cell {
        let cell = tableViewFriends.dequeueReusableCellWithIdentifier("Cell") as! Cell;

        var name: String = dataFriends[indexPath.row]["name_friend"] as! String

        cell.lblFriendName?.text = name.capitalizedString
        cell.imgFriend.image = UIImage(named: "homer.jpg")

        cell.imgRadio.image = UIImage(named: "radio-unselected")

        return cell;
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

        let cell:Cell = tableView.cellForRowAtIndexPath(indexPath) as! Cell
        cell.imgRadio.image = UIImage(named: "radio-selected")
        cell.backgroundColor = UIColor.redColor()


        NSLog("You selected cell number: \(indexPath.row)!")
    }
    func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {

        let cell:Cell = tableView.cellForRowAtIndexPath(indexPath) as! Cell
        cell.imgRadio.image = UIImage(named: "radio-unselected")
        cell.backgroundColor = UIColor.whiteColor()


         NSLog("You deselect cell number: \(indexPath.row)!")

    }



When I tap the row, the didSelectRowAtIndexPath worked very well, but if I tap again, didDeselectRowAtIndexPath don't run, and didSelectRowAtIndexPath worked again.

Why? I need when I tap on the first time on the row didSelectRowAtIndexPath, should be called, when I tap on the second time didDeselectRowAtIndexPath should be called

Answered by Alexandre Carmo in 29470022

Only to help others users, I fix the problem, I change the way, I do this:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
       
        let cell:CellList = tableView.cellForRowAtIndexPath(indexPath) as! CellList
       
        if(cell.imgRadio.image == UIImage(named: "radio-unselected")){
            cell.imgRadio.image = UIImage(named: "radio-selected")
            cell.backgroundColor = UIColor.lightGrayColor()
        }
        else{
            cell.imgRadio.image = UIImage(named: "radio-unselected")
            cell.backgroundColor = UIColor.whiteColor()
        }
       
    }

You should ask this question in the Cocoa Touch forum area, since it's not a Swift issue.


Note, however, that you are abusing selections, which are not supposed to indicate state. The cell should be unselected as soon as the tap is processed. In your case, where your cell has an on/off state indicated by an image, you should arrange for didSelectRowAtIndexPath to toggle the state.

Thanks for help, I put this quastion on cocoa touch.

Thansk for Thank you for the explanation, But I did not get what do you sad. How can I put the state on/off?

Accepted Answer

Only to help others users, I fix the problem, I change the way, I do this:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
       
        let cell:CellList = tableView.cellForRowAtIndexPath(indexPath) as! CellList
       
        if(cell.imgRadio.image == UIImage(named: "radio-unselected")){
            cell.imgRadio.image = UIImage(named: "radio-selected")
            cell.backgroundColor = UIColor.lightGrayColor()
        }
        else{
            cell.imgRadio.image = UIImage(named: "radio-unselected")
            cell.backgroundColor = UIColor.whiteColor()
        }
       
    }

Check in the interface builder if the property selection is set to 'No selection'. Change it to 'Single selection' or other option according to your needs.

This might be the reason why didSelect is not getting triggered. I hope it helps.

didDeselectRowAtIndexPath dont work
 
 
Q