首页 > 解决方案 > 使用类方法实例化 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 Cell。我们会面临任何与内存管理相关的问题吗?

标签: iosswiftuitableview

解决方案


简而言之,没有。

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

// 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
}


推荐阅读