簡體   English   中英

Tableview單元格字幕

[英]Tableview cell subtitles

使用此功能時,我的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")

故事板

老但是...

我認為問題是:

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

像這樣做:

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

}

如果這解決了您的問題,請將其標記為解決方案/升級。

最簡單的解決方案:

直接在表視圖中的Interface Builder中設計兩個單元格,將樣式和附件視圖設置為所需的值並添加標識符。

然后可以將代碼簡化為。

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

今天也遇到這個問題。 我認為原始的發帖人找到了答案,但是對於將來遇到此問題的其他人來說,這就是我解決的方法。 請注意,此鏈接/線程也說明了求解的其他方法。 如何在Swift中設置UITableViewCellStyleSubtitle和dequeueReusableCell?

環境: 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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM