【问题标题】:Same prototype cell with multiple UITableView(s)具有多个 UITableView(s) 的相同原型单元格
【发布时间】:2016-07-26 11:57:59
【问题描述】:

在 Storyboard 中,我可以定义一个(或多个)原型单元以与 UITableView 一起使用。

但是我需要将同一个原型单元与多个 UITableView(s) 一起使用?

有可能吗? 谢谢。

【问题讨论】:

    标签: ios swift uitableview uikit


    【解决方案1】:

    在UITableView 之间共享单元格的唯一方法是在 .xib 中将它们定义为单独的文件,并为它们提供重用标识符。然后在代码中将该文件/相应的类注册到UITableView。

    例如将其放在 tableViewController 的 viewDidLoad() 中:

    tableView.registerNib(UINib(nibName: "", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "")
    

    然后您就可以在cellForRowAtIndexPath 中照常继续了

    【讨论】:

    • 谢谢,请注意UINib初始化器中缺少参数
    • @appzYourLife 你是对的,谢谢。在没有 Xcode 的情况下从内存中键入,忘记了。
    【解决方案2】:

    你必须创建一个 nib 文件 (.xib),你可以通过 File -> New -> View (在用户界面部分下)

    然后设置你的 UITableViewCell 元素并设置一些reuseIdentifier,你也可以根据需要创建子类

    然后在您的 viewController 中(您想在其中使用此自定义 tableViewCell)

    在 viewDidLoad 中,您需要使用 tableView 注册该笔尖,例如,

    override func viewDidLoad() {
        // the .xib filename
        tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCellId")
    
    }
    

    然后在cellForRowAtIndexPath中,就可以像原型cell一样正常使用了,

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellId", forIndexPath: indexPath) as! Customcell
    
        // your custom code
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多