J'ai une tableView d'où j'insère 20 rangées avec retard à l'aide de la méthode Dispatchqueuse. Les 10 premières lignes apparaissent bien. Le problème commence avec le 11ème lorsque Xcode commence à déroger des rangées réutilisables. Dans Simulator, on dirait qu'il commence à insérer par 2 rangées presque à la fois (11ème + 12ème, puis 13ème + 14ème).
Je me demande pourquoi est-ce. Est-ce que DispatchQueue et TableView.DequeueReusableceCell est-il conflit? Et si oui comment organiser les choses correctement? P>
3 Réponses :
Essayez de placer le code à l'intérieur DispatchQueue.Main.Asyncaster code> dans
self.tableView.beginUpdates()
self.tableView.endUpdates()
Ne change pas vraiment rien. Pour autant que je sache, vous utilisez Débutumdates () et endupdates () lorsque vous souhaitez implémenter plusieurs modifications à la tableView. Mais merci de toute façon!
Votre code n'a pas fonctionné pour moi. Peut-être que vous avez manqué quelque chose à mentionner dans votre question. Mais avec les informations que j'ai comprises, j'ai fait une modification et maintenant elle fonctionne (testée sur iPhone X) comme prévu. Voici le code source complet de travail.
import UIKit class InsertCellViewController: UIViewController, UITableViewDataSource { var dataArray:Array<String> = [] let reusableCellId = "AnimationCellId" var timer = Timer() var index = -1 @IBOutlet weak var tableView: UITableView! // UIViewController lifecycle override func viewDidLoad() { super.viewDidLoad() tableView.register(UITableViewCell.self, forCellReuseIdentifier: reusableCellId) tableView.separatorStyle = .none } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) updateTableView() } // MARK : UITableViewDataSource func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: reusableCellId)! cell.textLabel?.text = dataArray[indexPath.row] return cell } // Supportive methods func updateTableView() { timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(updateCounting), userInfo: nil, repeats: true) } @objc func updateCounting(){ if index == 19 { timer.invalidate() } index += 1 let indexPath = IndexPath(row: index, section: 0) self.tableView.beginUpdates() self.dataArray.append(String(index)) self.tableView.insertRows(at: [indexPath], with: .fade) self.tableView.endUpdates() } }
Je pense qu'utiliser une minuterie code> est la meilleure solution dans votre cas d'utilisation:
private var cellCount = 0 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return cellCount } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = "Cell \(indexPath.row)" return cell } func addCells(count: Int) { guard count > 0 else { return } var alreadyAdded = 0 Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] t in guard let self = self else { t.invalidate() return } self.cellCount += 1 let indexPath = IndexPath(row: self.cellCount - 1, section: 0) self.tableView.insertRows(at: [indexPath], with: .fade) alreadyAdded += 1 if alreadyAdded == count { t.invalidate() } } } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) addCells(count: 20) }