Ralph simmonds
Ralph simmonds

Reputation: 55

How to modify the properties of only one custom cell of a table view?

I want to have a table view cell that contains a button that increments a label by 1 everytime that button is pressed. So if the button is pressed, the label displays: "1,2,3,4", etc. And I want each cell row to be independent from each other, meaning I only want the increment button in cell row 3 to modify the label of cell row 3 and same for cell row 4 and so on.

However my current code functionality does the following:

  1. When I press the increment button it changes the label for all cells not just the label the button is pressed in (I don't want this).

  2. When I scroll the table view all the labels change even when I haven't pressed the button in that row (I want don't want this either, I only want the label to change if its corresponding increment button has been pressed).

Can anyone help get my code or help me construct some code to help me achieve my goals?

Thanks in advance all help is highly appreciated!

My code:

 import UIKit


  var bookieFlavors = ["Chocolate Chip", "Sugar w/o icing", "Sugar w/ icing", "Peanut Butter", "Honey", "Shortbread", "Ginger", "Double Chocolate", "Macadamie Nut", "Oatmeal Raisin", "Snickerdoodle"]
  var labelAmount = Int()


 class FlavorsController: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var flavorTable: UITableView!



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int    
{
    return bookieFlavors.count

}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FlavorTableCell

    //flavor label configuration
    cell.flavorLabel.text = bookieFlavors[indexPath.row]



    //amount configuration
    cell.bookieAmount.text = "= \(amount)"



    return cell

  class FlavorTableCell: UITableViewCell {



@IBOutlet weak var flavorLabel: UILabel!

@IBOutlet weak var bookieButton: UIButton!
@IBAction func bookieButton(_ sender: UIButton) {
    for _ in 1...15 {

        bookieAmount.text = "= \(String(describing: amount))"
}
    labelAmount += 1
}


@IBOutlet weak var bookieAmount: UILabel!

Upvotes: 1

Views: 1499

Answers (1)

Nishant Bhindi
Nishant Bhindi

Reputation: 2252

You can not add button action in the tableview cell to do this. Please check below code for the solution.

import UIKit

class FlavorsController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var bookieFlavors = ["Chocolate Chip", "Sugar w/o icing", "Sugar w/ icing", "Peanut Butter", "Honey", "Shortbread", "Ginger", "Double Chocolate", "Macadamie Nut", "Oatmeal Raisin", "Snickerdoodle"]
var labelAmount = [Int]() // To keep track of the amount in each cell

@IBOutlet weak var flavorTable: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    for item in self.bookieFlavors {
        self.labelAmount.append(0) //Initialise with default value
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return bookieFlavors.count

}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FlavorTableCell

    //flavor label configuration
    cell.flavorLabel.text = bookieFlavors[indexPath.row]

    //amount configuration
    cell.bookieAmount.text = "= \(self.labelAmount[indexPath.row])"
    cell.bookieButton.tag = indexPath.row
    cell.bookieButton.addTarget(self, action: #selector(bookieButton(_:)), for: .touchUpInside)

    return cell
}

//To configure the button click and according changes
@IBAction func bookieButton(_ sender: UIButton) {
    self.labelAmount[sender.tag] = self.labelAmount[sender.tag] + 1
    let cell = self.flavorTable.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? FlavorTableCell
    cell?.bookieAmount.text = "= \(self.labelAmount[sender.tag])"
  }
}

class FlavorTableCell: UITableViewCell {

    @IBOutlet weak var flavorLabel: UILabel!

    @IBOutlet weak var bookieButton: UIButton!

    @IBOutlet weak var bookieAmount: UILabel!
 }

Upvotes: 1

Related Questions