1

I have working 2 buttons on tableview cell I got action with sender, but I need to change button images each click for example:

On load:

button1image = user.png
button2image = empty.png

If user click button 1:

button1image = empty.png
button2image = user.png

If user clicks button 2:

button1image = user.png
button2image = empty.png

In my tableviewcell:

  cell.button1.addTarget(self, action: #selector(ViewController.clickbutton1(_:)), forControlEvents: UIControlEvents.TouchUpInside)

  cell.button2.addTarget(self, action: #selector(ViewController.clickbutton2(_:)), forControlEvents: UIControlEvents.TouchUpInside)

In ViewController:

func clickbutton1(sender:UIButton) {

        let buttonRow = sender.tag
}

  func clickbutton2(sender:UIButton) {


        let buttonRow = sender.tag

}

Also I can change buttons inside images like this:

  sender.setImage(UIImage(named:"empty.png"), forState:UIControlState.Normal)

How can I do that?

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
  • What are you trying to achieve ?... it is not clear. – Roy K Sep 05 '16 at 14:21
  • @RoyK i added example if 's you can see top side , i need to connect 2 buttons in each cell if button 1 click will be change button 1 image to new and also button 2 image to old. need to change 2 image between 2 buttons – SwiftDeveloper Sep 05 '16 at 14:24

2 Answers2

1

You can try like this, create 2 string instance that contain image name and use that in cellForRowAtIndexPath like this way.

var button1image = "user.png"
var button2image = "empty.png"

Now in cellForRowAtIndexPath.

cell.button1.setImage(UIImage(named: button1image), forState: .Normal)
cell.button2.setImage(UIImage(named: button2image), forState: .Normal)

Now just change the value of variable inside button action and just reload the tableView.

func clickbutton1(sender:UIButton) {

    let buttonRow = sender.tag
    button1image = "empty.png"
    button2image = "user.png"
    self.tableView.reloadData()
}

func clickbutton2(sender:UIButton) {

    let buttonRow = sender.tag
    button1image = user.png
    button2image = empty.png
    self.tableView.reloadData()
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
1

Create a custom UITableViewCell and then you will be able to do whatever you want : https://stackoverflow.com/questions/33004189/swift-custom-cell-creating-your-own-cell-with-labels

Community
  • 1
  • 1
Roy K
  • 3,319
  • 2
  • 27
  • 43