Wael
Wael

Reputation: 411

Swift UitableViewcell with button

I have a strange behavior in the UITABLEVIEW.

I have a TableView with Button, what I wanted to do is when I clicked to a button in the tableView, I want the color border to change to red.

The problem is that the color is changing not only for the clicked button, but also for others row in the tableview:

Here is my implementation

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

    cell.Btn_AddProduct.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)


    return cell
}

@objc func btnAction(_ sender: MyButton) {

    sender.BorderColor = UIColor.red
    let point = sender.convert(CGPoint.zero, to: tbl_View_ChooseProduct as UIView)
    let indexPath: IndexPath! = self.tbl_View_ChooseProduct.indexPathForRow(at: point)
     let object = self.fetchedResultsController.object(at: indexPath)
    print (object.produit_name)
    print("row is = \(indexPath.row) && section is = \(indexPath.section)")
} 

As you can see in the picture below I have only clicked on the first button (Abricot) ==> other button has also automatically changed the border (Avocat) and many others.

enter image description here

Upvotes: 2

Views: 231

Answers (3)

McDonal_11
McDonal_11

Reputation: 4075

You have to store default Colors in Dictionary.

var orderColor = [Int : UIColor]()

override func viewDidLoad() {
super.viewDidLoad()

    for i in 0...29  // TotaL number of rows
    {
        orderColor[i] = UIColor.blue // DEFAULT color of the Button
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return 30
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! chooseProductTVC

   cell.Btn_AddProduct.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)

   cell.Btn_AddProduct.BorderColor = orderColor[indexPath.row] 

   cell.selectionStyle = .none
   return cell
}


@objc func btnAction(_ sender: MyButton) {


   let point = sender.convert(CGPoint.zero, to: tbl_View_ChooseProduct as UIView)
   let indexPath: IndexPath! = self.tbl_View_ChooseProduct.indexPathForRow(at: point)

    orderColor[indexPath.row] = UIColor.red

    tbl_View_ChooseProduct.reloadData()  

}

Upvotes: 0

Lal Krishna
Lal Krishna

Reputation: 16160

Since we reuse the cell for displaying, we have to modify the color every time after dequeing.

Add new property selected to product model.

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

    cell.Btn_AddProduct.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)
    let object = self.resultArray(at: indexPath.row)
    if object.selected
         cell.Btn_AddProduct.BorderColor = .red
    else
        cell.Btn_AddProduct.BorderColor = .blue

    return cell
}

@objc func btnAction(_ sender: MyButton) {
    sender.BorderColor = UIColor.red
    let point = sender.convert(CGPoint.zero, to: tbl_View_ChooseProduct as UIView)
    let indexPath: IndexPath! = self.tbl_View_ChooseProduct.indexPathForRow(at: point)
    let object = self.fetchedResultsController.object(at: indexPath)
    object.selected = true
    print (object.produit_name)
    print("row is = \(indexPath.row) && section is = \(indexPath.section)")
} 

Tip: You could use tableChooseProduct(iOS) instead of tbl_View_ChooseProduct.(android). This link may helpful.

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

This is because of cell dequeuing try to re set when you load the table , suppose here default is blue

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")  

  cell.Btn_AddProduct.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)

  if selected

    cell.Btn_AddProduct.BorderColor = UIColor.red

   else

   cell.Btn_AddProduct.BorderColor = UIColor.blue

  return cell

} 

Upvotes: 1

Related Questions