From 88da462434fea782ceb673c610bbc8dd3b96c8fa Mon Sep 17 00:00:00 2001 From: toshi0383 Date: Fri, 27 Aug 2021 15:35:10 +0900 Subject: [PATCH] Demonstrate a bug in willDisplay cell --- .../ReverseExtensionSample/MessageModel.swift | 5 +++- .../ViewController.swift | 29 ++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/ReverseExtensionSample/ReverseExtensionSample/MessageModel.swift b/ReverseExtensionSample/ReverseExtensionSample/MessageModel.swift index 8ec53f9..19f969a 100644 --- a/ReverseExtensionSample/ReverseExtensionSample/MessageModel.swift +++ b/ReverseExtensionSample/ReverseExtensionSample/MessageModel.swift @@ -29,7 +29,10 @@ struct MessageModel { let imageName: String let message: String let time: String - +} + +extension MessageModel { + init() { imageName = Const.imageNames[Int(arc4random_uniform(UInt32(Const.imageNames.count)))] message = Const.messags[Int(arc4random_uniform(UInt32(Const.messags.count)))] diff --git a/ReverseExtensionSample/ReverseExtensionSample/ViewController.swift b/ReverseExtensionSample/ReverseExtensionSample/ViewController.swift index bc22da9..be6ab32 100644 --- a/ReverseExtensionSample/ReverseExtensionSample/ViewController.swift +++ b/ReverseExtensionSample/ReverseExtensionSample/ViewController.swift @@ -13,7 +13,13 @@ class ViewController: UIViewController { @IBOutlet weak var tableView: UITableView! - fileprivate var messages: [MessageModel] = [] + private var sections: [[MessageModel]] = (1...10) + .map { i -> [MessageModel] in + (0.. Int { + sections.count + } + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return messages.count + sections[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) + let messages = sections[indexPath.section] (cell as? TableViewCell)?.configure(with: messages[indexPath.row]) return cell } + + func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { + let messages = sections[indexPath.section] + let message = messages[indexPath.row] + print("willDisplay: \(message)") + } }