A ação do botão no UITableViewCell personalizado afeta outras células
eu tenho umTablewView que tem protótipoUITableViewCell que tem sua própria classe,
Classe UITableViewCell personalizada
import UIKit
class H_MissingPersonTableViewCell: UITableViewCell {
@IBOutlet weak var strName: UILabel!
@IBOutlet weak var imgPerson: UIImageView!
@IBOutlet weak var Date1: UILabel!
@IBOutlet weak var Date2: UILabel!
@IBOutlet weak var MainView: UIView!
@IBOutlet weak var btnGetUpdates: UIButton!
@IBOutlet weak var btnComment: UIButton!
@IBOutlet weak var btnReadMore: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
O TableView carrega perfeitamente e o dataSource e o delegado funcionam bem. No entanto, quando clico em um BOTÃO em IndexPath.row = 0 (zero) e, em seguida, role para baixo, veria aleatórias (?) Ou outras células também sendo destacadas como resultado de clicar em BUTTON na linha 0 ...
Aqui está o meu código CellForRowAtIndexPath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println("called")
var cell : CustomCell
cell = tableView.dequeueReusableCellWithIdentifier("CustomCellID", forIndexPath: indexPath) as! CustomCell
cell.strName.text = self.names[indexPath.row]
cell.imgPerson.image = UIImage(named: "\(self.persons[indexPath.row])")!
cell.btnGetUpdates.layer.borderColor = UIColor.darkGrayColor().CGColor
cell.btnGetUpdates.layer.borderWidth = 1
cell.btnGetUpdates.layer.cornerRadius = 5.0
cell.btnComment.layer.borderColor = UIColor.darkGrayColor().CGColor
cell.btnComment.layer.borderWidth = 1
cell.btnComment.layer.cornerRadius = 5.0
cell.btnReadMore.layer.borderColor = UIColor.darkGrayColor().CGColor
cell.btnReadMore.layer.borderWidth = 1
cell.btnReadMore.layer.cornerRadius = 5.0
cell.dateMissingPersonPlace.text = self.MissingPeoplePlace[indexPath.row]
cell.dateMissingPersonSince.text = self.MissingPeopleSince[indexPath.row]
cell.btnGetUpdates.tag = indexPath.row
cell.btnGetUpdates.addTarget(self, action: "GetUpdatesButton:", forControlEvents: .TouchUpInside)
return cell
}
no meucell.btnGetUpdates , Coloquei uma ação, como você pode ver na última parte do meu código CellForIndex,GetUpdatesButton:
Este é o código:
func GetUpdatesButton(sender: UIButton){
println("Button Clicked \(sender.tag)")
var sen: UIButton = sender
var g : NSIndexPath = NSIndexPath(forRow: sen.tag, inSection: 0)
var t : CustomCell = self.myTableView.cellForRowAtIndexPath(g) as! CustomCell
t.btnGetUpdates.backgroundColor = UIColor.redColor()
}
O problema é que NÃO SOMENTE o botão no índice 0 está sendo destacado, mas eu também veria botões aleatórios de outros índices / linhas sendo destacados enquanto rolava meu tableView para baixo.
Onde eu errei, como posso atualizar apenas o botão em que cliquei ... e não os outros botões ..
Eu tenho 20 itens (20 linhas) e quando eu clico no botão na linha 0, as linhas 3, 6, 9 .... também estão com os botões destacados.
ROW 0, clique no botão
LINHA 3, o botão também clicou, embora eu realmente não tenha clicado nele.