【问题标题】:How to add two custom cell into tableview in storyboard in Swift?如何在 Swift 的故事板中将两个自定义单元格添加到 tableview 中?
【发布时间】:2014-08-06 10:57:27
【问题描述】:

我有一个带有两个不同自定义单元格的 tableView。一个单元格有一个开关,另一个有一个图像,我有两个单独的单元格自定义类,标识符..但我看不到这个。我不知道我是否需要将故事板中的配置更改为动态或其他内容。我可以显示两个不同的自定义单元格

谢谢!

【问题讨论】:

  • 你在用故事板吗
  • 在 swift 中不会有任何区别,因为您将与情节提要进行交互并在那里定义您的原型

标签: swift tableview xcode6


【解决方案1】:

检查显示每个特定自定义单元格的标准,然后根据需要转换到该单元格:

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {

    if (criteria for cell 1) {
        let cell = tableView!.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as? Cell1
        return (cell)
    }
    else {
        let cell = tableView!.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as? Cell2
        return (cell)
    }
}

斯威夫特 3

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath -> UITableViewCell {

    if (criteria for cell 1) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! Cell2
        return cell
    }
}

【讨论】:

    【解决方案2】:
     override func tableView(tableView: UITableView?,cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
    
    if (...) {
        let cell : Mycell1  = tableView!.dequeueReusableCellWithIdentifier("Mycell1", forIndexPath: indexPath) as? Mycell1
        return cell
    }
    else {
        let cell : Mycell2 = tableView!.dequeueReusableCellWithIdentifier("Mycell2", forIndexPath: indexPath) as? Mycell2
        return cell
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 2012-12-06
      相关资源
      最近更新 更多