Reputation: 15
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
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
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
Reputation: 757
you should create a selectedArray that maintains the button selection track
var selectedButtonsArray = [{
isSelected = true
},
{isSelected = false}
]
Hope this may help you, Thank you
Upvotes: 0
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
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