【问题标题】:Tableview cell subtitles表格视图单元格字幕
【发布时间】:2017-02-19 11:31:50
【问题描述】:

当我使用这个时,我的 tableview 单元格字幕没有显示:

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

var cell:UITableViewCell?

if tableView.tag == 1 {

    guard let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") else {
        return UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell")
    }
    latestCell.textLabel?.text = latest[indexPath.row]

    latestCell.detailTextLabel?.text = latestSub[indexPath.row]

    latestCell.accessoryType = .disclosureIndicator

    return latestCell
   }
}

但是如果我使用这个:

else if tableView.tag == 2 {

    let olderCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "olderCell")



    olderCell.textLabel?.text = older[indexPath.row]

    olderCell.detailTextLabel?.text = olderSub[indexPath.row]

    olderCell.accessoryType = .disclosureIndicator

    return olderCell
  } else {
    return cell!
  }
}

字幕加载完美,但在我关闭应用程序并重新加载视图后,应用程序会自动退出,而不会提供崩溃日志或将我带到调试选项卡。

我知道数据来自的数组很好,我认为我已经在情节提要中正确设置了所有内容。关于这个主题已经发布了很多类似的问题,但它们似乎都归结为忘记将 cellStyle 设置为 .subtitle。提前感谢我得到的任何帮助!

顺便说一句。我的常规单元格标题就像我想要的那样工作。没问题。

编辑:

我认为问题在于我可以毫无问题地创建一个默认样式的单元格。但是当我尝试将样式设置为 .subtitle 时,它​​第一次正确加载,但第二次打开时,它崩溃了。有没有办法将这两个声明一起使用,而不会相互消除;

guard let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") else {
    return UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell")
}

和:

let latestCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "latestCell")

【问题讨论】:

  • 在你守卫的时候,你甚至没有设置它的属性就返回了单元格。第二个代码不重用单元格(不推荐)。
  • 朋友我可以问你吗?你在使用两个 tableview 吗?
  • 是的,我用了两个。
  • 我解决了这个问题,结果发现 tableview 没有正确重新加载。在我修复了激活 tableview 的语句的顺序之后,它修复了它。谢谢大家的回复!

标签: ios iphone swift uitableview tableview


【解决方案1】:

旧但...

我认为问题是:

guard let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") else {
    return UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell")
}

将返回一个“空”单元格。

所以..就像:

var latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell")

if latestCell == nil {
   latestCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "latestCell")
}

// your stuff

【讨论】:

    【解决方案2】:

    今天也遇到了这个问题。我假设原始发布者找到了答案,但对于将来遇到此线程的其他人来说,这就是我解决这个问题的方法。请注意,此链接/线程也解释了解决的其他方法。 How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?

    环境: Swift 5.0 和 Xcode 版本 10.2.1。

    说明:我通过继承 UITabelViewCell 并使用 .subtitle 类型对其进行初始化来解决这个问题(参见下面的代码)。注意 super.init 方法中的 .subtitle。

    一旦你有了子类,不要忘记将 CustomCell 注册到你的 tableView 并在你的 tableView 方法 cellForRowAt 中向下转换为 CustomCell。

    class CustomCell: UITableViewCell {
    
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    这里是 cellForRowAt 方法的代码,它具有 titleLabel 和 detailTextLabel(subtitle) 属性。请注意向下转换的“as!CustomCell”。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 
    UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: 
    indexPath) as! CustomCell
    
                cell.textLabel?.text = someArray[indexPath.row].title // Note someArray would have to be replaced with your array of strings.
                cell.detailTextLabel?.text = someArray[indexPath.row].subtitle // Note someArray would have to be replaced with your array of strings.
    
            return cell
        }
    

    【讨论】:

      【解决方案3】:

      这样做:

      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      
          var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("latestCell") as UITableViewCell
      
          cell.textLabel?.text = latest[indexPath.row]
          cell.detailTextLabel?.text = latestSub[indexPath.row]
          cell.accessoryType = .disclosureIndicator
      
          return cell
      
      }
      

      如果这解决了您的问题,请将其标记为解决方案/点赞。

      【讨论】:

      • 恐怕这并没有将cellStyle设置为副标题,所以没有什么区别。
      【解决方案4】:

      最简单的解决方案:

      在界面生成器中直接在表格视图中设计两个单元格,将样式和附件视图设置为您想要的值并添加标识符。

      那么代码就可以简化为。

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         switch tableView.tag {
         case 1:
         let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell" for: indexPath)
            latestCell.textLabel!.text = latest[indexPath.row]
            latestCell.detailTextLabel!.text = latestSub[indexPath.row]
            return latestCell
      
         case 2:
            let olderCell = tableView.dequeueReusableCell(withIdentifier: "olderCell" for: indexPath)
            olderCell.textLabel!.text = older[indexPath.row]
            olderCell.detailTextLabel!.text = olderSub[indexPath.row]
            return olderCell
      
         default: 
            fatalError("That should never happen")
         }
      
      }
      

      由于单元格预定义为字幕样式,textLabeldetailTextLabel 都保证存在并且可以安全地展开。

      但是,单元格之间的显着差异是什么。从给定的代码中,您实际上可以使用一个单元格(标识符cell)。这可以使代码更短:

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
         let cell = tableView.dequeueReusableCell(withIdentifier: "cell" for: indexPath)
      
         if tableView.tag == 1 {
            cell.textLabel!.text = latest[indexPath.row]
            cell.detailTextLabel!.text = latestSub[indexPath.row]
         } else {
            cell.textLabel!.text = older[indexPath.row]
            cell.detailTextLabel!.text = olderSub[indexPath.row]
         }
         return cell
      }
      

      【讨论】:

      • 这不会将样式设置为字幕,所以没有运气,我已经在情节提要中正确设置了我的单元格。但是感谢您的尝试!
      • 如果在 Interface Builder 中设置了样式,它必须出现在重用的单元格中。
      • 你这是什么意思?对不起,我不太明白。如果您指的是故事板单元格中设置的样式,那么是的,它会出现在那里。
      • 是的,如果你在storyboard中设置了样式,它就会出现,你不需要在代码中设置。但是你必须在两个表格视图中设计单元格。
      • 我编辑了帖子,有一个指向我的故事板屏幕截图的链接,我设置了两个原型单元格,它仍然无法正常工作。
      【解决方案5】:
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") //replace "Cell" with your identifier
          cell.textLabel = yourTitleArray[indexPath.row]
          cell.detailTextLabel = yourSubtitleArray[indexPath.row]
          return cell!
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 2018-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多