0

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)
   }
}
return true
  • 7,839
  • 2
  • 19
  • 35
Haasith Sanka
  • 15
  • 1
  • 7
  • 1
    Your are dequeuing the cells and making new one every time you scroll, I don't think you can achieve what you want with that. There is a way around though, you can set certain buttons selected using indexPath. – Akshansh Thakur Jul 11 '16 at 05:16
  • check this http://stackoverflow.com/questions/29507770/maintain-state-of-custom-button-in-uitableview-cell-when-scrolls ... it describes some steps may be helpful to you – Bhavin Bhadani Jul 11 '16 at 05:23

5 Answers5

5

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

Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
1

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.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

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

Chandan
  • 747
  • 7
  • 17
0

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
    }
Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
0

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)))
Saurabh Jain
  • 1,688
  • 14
  • 28