提问者:小点点

使用class方法实例化UITableViewCell会不会出现内存管理问题?


    extension SettingsTableViewController {
         override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
             let cell = SettingsTableViewCell.getSettingTableViewCell(tableview: tableView, indexPath: indexPath)
             return cell
         }
    }
    class SettingsTableViewCell: UITableViewCell {
         static let identifier = "SettingsTableViewCell"
         public class func getSettingTableViewCell(tableview: UITableView, indexPath: IndexPath) -> SettingsTableViewCell {
             if let cell = tableview.dequeueReusableCell(withIdentifier: self.identifier, for: indexPath) as? SettingsTableViewCell {
                 return cell
             }
             return SettingsTableViewCell()
         }
     }

通过使用此方法实例化tableView单元格。 我们将面临任何与内存管理有关的问题吗?


共1个答案

匿名用户

总之,没有。

但是,当您向tableview注册cell类时,不需要手动执行此操作:

// e.g. in viewDidLoad
tableView.register(SettingsTableViewCell.self, forCellReuseIdentifier: "cell")

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SettingsTableViewCell
    return cell
}