私はこれを行うことができる方法を示すために小さなプロジェクトを作成しました。 Git it enter link description here
どのように動作しますか?
プロパティは、最新のクリックされた細胞を保存する場所を宣言されています:
var lastSelectedCells = [IndexPath]()
我々はその配列の先頭に最新のクリックされたセルを持つようにしたいので、ここで我々は法に何をすべきか「DidSelectCellAtRowIndexPath」です:
lastSelectedCells.insert(indexPath, at: 0)
我々はまた、上記のは、私たちが実行した直後に、(現在のものを含む)の前にクリックされたセルを更新したいので:
tableView.reloadRows(at: lastSelectedCells, with: .automatic)
(ビットプロジェクトに比べて簡略化)を以下のようにロジックの残りの部分は、「CellForRowAtIndexPath」法である:現在indexPathがアレイ内にある場合、我々は設定:ある
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NiceCell") as! MyBeautifulCell
if let index = lastSelectedCells.index(of: indexPath) {
cell.centeredLabel?.text = "Cell tapped \(index) times ago!"
} else {
cell.centeredLabel?.text = "Uhmpf, this cell was never tapped!"
}
return cell
}
セルのテキストがそのインデックスになるようにします。それ以外の場合は、わかりやすいテキストを設定します。
私はあなたのプロジェクト全体を認識していませんが、これはあなたがしようとしていることを達成するのに十分なはずです。
あなたが言うので:私は次のようにメソッドを変更することがあり ...前のセル内のデータをクリアする必要があります。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "NiceCell") as? MyBeautifulCell else {
fatalError("Table Configuration Error!")
}
if let index = lastSelectedCells.index(of: indexPath) {
switch index {
case 0:
cell.centeredLabel?.text = "Cell tapped"
default:
cell.centeredLabel?.text = "CLEARED"
}
} else {
cell.centeredLabel?.text = "Uhmpf, this cell was never tapped!"
}
return cell
}
あなたはこの道を行けば、reloadRows実装のみ秒をリロードすることができます最後の行ですが、これが最適化です。
どちらのセルにもアクセスする必要はありません。テーブルのデータソースで使用されるデータモデルを更新し、影響を受けるインデックスパスをリロードするようにテーブルビューに指示します。 – rmaddy
ビューに*存在しないセルは存在しません。 – vadian