簡體   English   中英

填充 UITableViewCell 時使用未解析的標識符“單元格”

[英]Use of unresolved identifier 'cell' when populating UITableViewCell

我是 Swift 的新手,正在學習系列教程。 該教程似乎跳來跳去,沒有遵循逐步格式,這導致我的代碼中出現了幾個錯誤。 我已經設法調試了其中的大部分,但這個不會消失。

我不明白為什么即使上面定義了“單元格”也無法識別。 這可能是一個非常簡單的問題,或者是特定於新 Xcode 更新(運行 Xcode 9.2)的問題。

我正在嘗試使用代碼中的用戶圖像填充 UITableViewCell。

任何幫助將非常感激

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "ShareSomethingCell") as? ShareSomethingCell {
            cell.configCell(userImageUrl: currentUserImageUrl)
            cell.shareBtn.addTarget(self, action: #selector(toCreatePost(_:)), for: .touchUpInside)
        }
        return cell
    }
    return UITableViewCell()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ShareSomethingCell") as? ShareSomethingCell 
    if indexPath.row == 0 {
        cell.configCell(userImageUrl: currentUserImageUrl)
        cell.shareBtn.addTarget(self, action: #selector(toCreatePost(_:)), for: .touchUpInside)
    }
    return cell
}

我不明白為什么即使上面定義了“單元格”也無法識別

因為:

  • 花括號構成一個范圍 在作用域內聲明的內容,留在該作用域內。

  • if let (條件綁定)以一種特殊的方式工作:它在條件的大括號聲明let變量。

所以,把它們放在一起,你會得到這個:

    if let cell = ... {
        // `cell` is defined HERE...
    }
    // `cell` is NOT defined here...
    return cell // error

暫無
暫無

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

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