ここで少し助けが必要です。私はスウィフトの新人です。ここに私の問題がある。再利用可能なセルがprepareForReuse関数を呼び出さない
私のUITableViewのデータを取得するとき、私はURLから画像データを呼び出しています。したがって、再利用されたセルを取得するときに若干の遅延があり、結果としてセルに半分の古いデータが表示されます。私はfunc prepareForReuseを呼び出してプロパティをリセットしようとしましたが、動作していないようです。どんな助けもありがとう!
セルを呼び出すときにここに私のコードです:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.alpha = 0
let book = books[indexPath.row]
cell.textLabel?.text = book.bookTitle
cell.detailTextLabel?.text = book.postURL
let url = URL(string: book.postPicture)
DispatchQueue.global().async {
let data = try? Data(contentsOf: url!)
DispatchQueue.main.async {
cell.alpha = 0
cell.backgroundView = UIImageView(image: UIImage(data: data!))
UIView.animate(withDuration: 0.5, animations: {
cell.alpha = 1
})
}
}
cell.contentView.backgroundColor = UIColor.clear
cell.textLabel?.backgroundColor = cell.contentView.backgroundColor;
cell.detailTextLabel?.backgroundColor = cell.contentView.backgroundColor;
func prepareForReuse(){
cell.alpha = 0
cell.backgroundView = UIImageView(image: UIImage(named: "book.jpg"))
}
return cell
}
あなたは 'prepareForReuse'をそのようにオーバーライドすることはできません。 'UITableViewCell'をサブクラス化し、それをサブクラス宣言でオーバーライドする必要があります。そして、 'dequeueReusableCell(withIdentifier:for:)'の結果をサブクラスに型キャストする必要があります。また、技術的には、まずディスクに画像を非同期にダウンロードしてから、ユーザーがスクロールしながらディスクから画像ビューに画像をロードする必要があります。 –
これはどのように動作するかの例がありますか? – Gerrit