Reputation: 29
I got a tableview cell inside that i have image view , label and a ui button , i need to change the button title text and background colour on click . But when i try to do button from multiple cell is having the same effect . Please do help me !
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! subTableViewCell
cell.catNameLabel.text = categoryNameArray[indexPath.row]
cell.descLabel.text = descriptionArray[indexPath.row]
cell.freqLabel.text = frequency[indexPath.row] + " at " + frequencyTime[indexPath.row]
let url = "http://wiinnova.com/tenly/image/category_image/\(categoryImageArray[indexPath.row])"
cell.img.imageFromServerURL(urlString: url)
cell.button.tag = indexPath.row
return cell
}
Upvotes: 3
Views: 6778
Reputation: 6082
First of all you should take one Array
for manage clicked/changed UIButton
for reusableCell
otherwise if you scroll your tableView Up-Down then It will be remove effect
I'm giving my logic.
1) Take one mutable Array name is temArray
(Size equal to your tableView's Row) and it has 0 value for each index. you can do it by easy repeat value 0.
2) in you cellForRowAtIndexPath
datasource method check
if temArray[indexPath.row] == 0 {
/// Write code for default UIButton - That has normal behavior means not changed title and no set background color
}
else {
/// Write code for What you want to keep UIButton title and background color
}
cell.yourButtonObject.tag = indexPath.row
cell.yourButtonObject.addTarget(self, action:#selector(handleButtonClicked(_:)), for: .touchUpInside).
3) And add handleButtonClicked
method and change temArray
value
func handleButtonClicked(_ sender: UIButton) {
let myIndexPath = NSIndexPath(row: sender.tag, section: 0)
let cell = tblViewPref.cellForRow(at: myIndexPath as IndexPath)
if temArray[sender.tag] == 0 {
/// You can access your button by
// cell.myButton..... change here text and background color
// And change "temArray"
temArray[sender.tag] = 1
}
else {
/// You can access your button by
// cell.myButton..... change here text and background color
// And change "temArray"
// SET DEFULT BUTTON TITLE AND BACKGROUND COLOR
temArray[sender.tag] = 0
}
}
So when you scroll your table Up-down temArray
will be managed by it's value at indexPath
in cellForRowAtIndexPath
Datasource method.
Upvotes: 3
Reputation: 4824
You don't need to assign button.tag
at all, Here is the way:
First of all add target
to your button. Like:
cell.button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside).
Then:
//This function will be called for each button click.
func handleRegister(sender: UIButton) {
let buttonPosition = sender.convert(CGPoint.zero, to: YOUR_TABLE_VIEW)
let indexPath: IndexPath? = YOUR_TABLE_VIEW.indexPathForRow(at: buttonPosition)
let cell = YOUR_TABLE_VIEW.cellForRow(at: indexPath as IndexPath)
//Now change the text and background colour
cell.button.setTitle("Button Title",for: .normal)
cell.button.backgroundColor = UIColor.blueColor()
//...
}
To keep the track of your changed button background and title you will need an array as @Tofaani Kaanudo's answer has suggested.
Upvotes: 3
Reputation: 6459
Set the title and the background color properties of the button in tableview's didSelectRowAtIndexPath
delegate
cell.button.setTitle("title", for: .normal)
cell.button.backgroundColor = .darkGray
Upvotes: 0