Reputation:
This is method for actions of buttons
@objc func recived()
{
kind = false
self.viewDidLoad()
self.viewWillAppear(true)
}
@objc func paid()
{
kind = true
self.viewDidLoad()
self.viewWillAppear(true)
}
Here tableView indexPath method
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView .dequeueReusableCell(withIdentifier: "kindCell", for: indexPath) as! KindTableViewCell
if kind == true {
cell.recivedButton.addTarget(self, action:#selector (recived), for: UIControlEvents.touchUpInside)
cell.recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: UIControlState.normal)
cell.paidButton.setImage(#imageLiteral(resourceName: "red off"), for: UIControlState.normal)
}
else{
cell.paidButton.addTarget(self, action: #selector (paid), for: UIControlEvents.touchUpInside)
cell.paidButton.setImage(#imageLiteral(resourceName: "red on"), for: UIControlState.normal)
cell.recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: UIControlState.normal)
}
return cell
}
Upvotes: 0
Views: 89
Reputation: 53092
You should handle this logic in the cell itself. Hook your outlets and actions up in Interface Builder, and then…
class KindTableViewCell: UITableViewCell {
@IBOutlet private weak var receivedButton: UIButton!
@IBOutlet private weak var paidButton: UIButton!
@IBAction func received(_ sender: UIButton) {
recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: .normal)
paidButton.setImage(#imageLiteral(resourceName: "red off"), for: .normal)
}
@IBAction func paid(_ sender: UIButton) {
paidButton.setImage(#imageLiteral(resourceName: "red on"), for: .normal)
recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: .normal)
}
}
Notice how the button outlets are private
- this removes the temptation to update them in the view controller. Let the cell update its own state.
If you need to do additional work in your view controller, add a delegate protocol to your table view cell…
protocol KindTableViewCellDelegate: class {
func kindTableViewCellDidTapReceive(_ kindTableViewCell: KindTableViewCell)
func kindTableViewCellDidTapPaid(_ kindTableViewCell: KindTableViewCell)
}
class KindTableViewCell: UITableViewCell {
// Outlets
var delegate: KindTableViewCellDelegate?
@IBAction func received(_ sender: UIButton) {
// Update button state
delegate?.kindTableViewCellDidTapReceive(self)
}
@IBAction func paid(_ sender: UIButton) {
// Update button state
delegate?.kindTableViewCellDidTapPaid(self)
}
}
Then in your table view controller…
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView .dequeueReusableCell(withIdentifier: "kindCell", for: indexPath) as! KindTableViewCell
cell.delegate = self
return cell
}
Upvotes: 1
Reputation: 3494
You can add multiple images on button according to states:
1: For normal state
cell.recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: UIControlState.normal)
1: For selected state
cell.recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: UIControlState.selected)
Now, You can handle button states, button auto select the images according to button states, You have to add this to custom class like this:
override func awakeFromNib() {
super.awakeFromNib()
cell.recivedButton.setImage(#imageLiteral(resourceName: "green off"), for: UIControlState.normal)
cell.recivedButton.setImage(#imageLiteral(resourceName: "green on"), for: UIControlState.selected)
cell.paidButton.setImage(#imageLiteral(resourceName: "red off"), for: UIControlState.normal)
cell.paidButton.setImage(#imageLiteral(resourceName: "red on"), for: UIControlState.selected)
}
Now , You need to handle button selected states like this in button target methods:
sender.isSelected = !sender.isSelected
Or can check condition that paidButton
is clicked then paid button isSlected
is true and recivedButton
isSelected
is false
Example:
@objc func recived(_ sender: UIButton)
{
sender.isSelected = !sender.isSelected
paidButton.isSelected = sender.isSelected
}
@objc func paid(_ sender: UIButton)
{
sender.isSelected = !sender.isSelected
recivedButton.isSelected = sender.isSelected
}
Upvotes: 0