1回目にスクロールアップまたはダウンする値が1回目に間違った値に変更されるたびに、正しい値が表示されますが、スクロール後は毎回変更されます。テーブルビューのセルのラベルがスクロールした後に変更され、間違った値を取得して解決する方法はありますか?
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
//creating a cell using the custom class
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ShowPercentageAttandanceTableViewCell
cell.rollNo.text = String(describing: self.mainArrayRoll[indexPath.row])
var location = Int()
for item in self.rollPercentage {
if item.rollCall == indexPath.row {
location = item.absentCount
cell.percentage.text=String(location)
}
else if cell.percentage.text==("Label")
{
cell.percentage.text=String("0")
}
}
return cell
}
ここにコードがあります。
[![ここで画像2のラベルはテキスト= 2で、灰色のラベルはテキスト0ですが、画像2 [![画像2のラベルはスクロール後に変更されますが、 2] [2]
を '; 'indexPath.row'でインデックスされた配列からテーブルのデータにアクセスする必要があります – Paulw11
古典的なセルの再利用の問題。場合によっては 'else'節が' 'Label ''であるかどうかをチェックしているので、再利用されたセルの '' cell.percentage.text''を設定していません。 'else {cell.percentage.text =" 0 "}'を試してください。 – vacawama
@vacawamaあなたが言ったことを試しましたが、すべてのラベルが0として表示されます。どうしてか分かりません。 –