Haasith Sanka
Haasith Sanka

Reputation: 15

How to make UIButton stay selected in a tableview when scrolled down

I have button in cell.swift file which has a button on it and this is the target function.

If I select the button and scroll down and go back up it get unselected again.

I've tried multiple variations of setSelected and set highlighted but nothing is working.

func selectRecipient(sender:UIButton) {
    if (!selectToShareButton.selected) {
        selectToShareButton.selected = true
        selectToShareButton.setImage(UIImage(named: "checkSelected"), forState: .Selected)  
    } else {
        selectToShareButton.selected = false
        selectToShareButton.setImage(UIImage(named: "checkUnselected"), forState: .Normal)
   }
}

Upvotes: 0

Views: 389

Answers (5)

Saurabh Jain
Saurabh Jain

Reputation: 1698

You should manage the model of cell:

func BtnPressed(sender: AnyObject) {
    var point: CGPoint = sender.convertPoint(CGPointZero, toView: self.categoryTable)
    var index: NSIndexPath = self.categoryTable(forRowAtPoint: point)
    var cell: UITableViewCell = self.categoryTable.cellForRowAtIndexPath(index)
    category.manageCell(index.row)
    self.categoryTable.reloadRowsAtIndexPaths([index], withRowAnimation: .Automatic)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if category.indexArray.containsObject(Int(indexPath.row)) {
        checkBtn.setImage(UIImage(named: "ic_check"), forState: .Normal)
    }
    else {
        checkBtn.setImage(UIImage(named: "ic_uncheck"), forState: .Normal)
    }
    return cell
}

func manageCell(index: Int) {
    // index array mutable array
    if !indexArray.containsObject(Int(index)) {
        indexArray.append(Int(index))
    }
    else {
        indexArray.removeAtIndex(indexArray.indexOf(Int(index)))

Upvotes: 0

Akshansh Thakur
Akshansh Thakur

Reputation: 5341

Elaborating on Hossam's answer,

You can create an array of boolean values that indicate whether a cell is selected or not.

var selectedBoolArray = [Bool]()

You can add target for you button so that it fires a method everytime this button is tapped:

override func viewDidLoad() {
    super.viewDidLoad()

   /// If your button is referenced from storyboard. Or add target to each button somehow.
    YourButton.addTarget(self, action: #selector(YourClass.methodToSelect(_:)), forControlEvents: .TouchUpInside)

}


func methodToSelect(sender: UIButton)
{
let index = sender.tag 
selectedBoolArray[index] = true
}

then in your cell for cellforrowatindexpath method, you can set these bools to false or true like so:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)


        if selectedBoolArray[indexPath.row] {
           // set you button  selected, cell.button.selected = true?
        }


    YourButton.tag = self.indexPath.row

        return cell
    }

Upvotes: 0

Chandan
Chandan

Reputation: 757

you should create a selectedArray that maintains the button selection track

 var selectedButtonsArray = [{
isSelected = true
},
{isSelected = false}
]
  1. add a object in array when button selected
  2. remove object from array when deselected
  3. In cell for row delegate method show button according the array

Hope this may help you, Thank you

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27448

This is because table view reuses cell and dequeuing it continuously with scroll. So everytime your cell created and destroyed when you scroll so all the property will set to default which you are setting in cellforrow or in cell's custom class if it available.

So, you can manage this something like, You can set tag to your button as indexpath.row and when you select button set some flag or save it's tag somewhere and put condition in cellforrow that if button's tag is from saved tags than make it selected.

Upvotes: 1

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7123

You should keep the state of your buttons in an array and in datasource function cellForRowAtIndexPath read the state and configure your cell button.

Upvotes: 5

Related Questions