私のセルのボタンについて問題があります。 私のテーブルでは、各行は画像、いくつかのラベル、ボタンで構成されています。 ボタンにチェックマークが付いています。クリックすると、ボタンの画像が変わります。 問題は、別のボタンのイメージが理由もなく変更されることです。 このエラーは、セルが再利用されたために発生します。セルの再利用がうまくいかない - TableView
私は、TableViewCellでprepareForReuse
メソッドを使用しようとしましたが、何も起こりません。私もselectedRowAt
で試しましたが、結果はありませんでした。私を助けてください。
画像1:
画像2:
これは私のcustom Cell:
override func prepareForReuse() {
if checkStr == "uncheck"{
self.checkBook.setImage(uncheck, for: .normal)
} else if checkStr == "check"{
self.checkBook.setImage(check, for: .normal)
}
}
func isPressed(){
let uncheck = UIImage(named:"uncheck")
let check = UIImage(named: "check")
if self.checkBook.currentImage == uncheck{
checkStr == "check"
} else self.checkBook.currentImage == check{
checkStr == "uncheck"
}
}
で私のFUNCです私
tableView
で
:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell: ListPropertyUserCell = tableView.cellForRow(at: indexPath) as! ListPropertyUserCell
let uncheck = UIImage(named:"uncheck")
let check = UIImage(named: "check")
if selectedCell.checkBook.imageView?.image == uncheck{
selectedCell.checkStr = "check"
} else if selectedCell.checkBook.imageView?.image == check{
selectedCell.checkStr = "uncheck"
}
}
セル自体ではなく、テーブルビューコントローラでセル選択状態を追跡する必要があります。あなたはここで答えの一つを使うことができます - http://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510鉱山またはJacob Kingさんがタップイベントをテーブルビューコントローラに戻す – Paulw11
@Carloは自分の問題を解決しようとしています。 –