簡體   English   中英

斯威夫特:無法在帶有節的表中使具有標識符的單元格出隊

[英]Swift: unable to dequeue a cell with identifier in tables with sections

在我的iOS應用中,我有一個包含4個部分的表格。 我為每個部分創建了一個字幕單元,每個單元都有自己的標識符,如您在屏幕截圖中所見:

在此處輸入圖片說明

問題是,當我需要通過使用其標識符創建該單元格在該部分中插入一個單元格時,出現一個異常,說“無法使具有標識符的單元格出隊...”,這是我的方法:

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

        var cell:UITableViewCell?
        var selectedStatus:String

        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCellWithIdentifier("OpenTasks",
                forIndexPath: indexPath) as! UITableViewCell
            selectedStatus = "Aperti"
            break
        case 1:
            let cell = tableView.dequeueReusableCellWithIdentifier("ClosedTasks",
                forIndexPath: indexPath) as! UITableViewCell
            selectedStatus = "Chiusi"
            break
        case 2:
            let cell = tableView.dequeueReusableCellWithIdentifier("ExpiredTasks",
                forIndexPath: indexPath) as! UITableViewCell
            selectedStatus = "Scaduti"
            break
        case 3:
            let cell = tableView.dequeueReusableCellWithIdentifier("SuspendedTasks",
                forIndexPath: indexPath) as! UITableViewCell
            selectedStatus = "Sospesi"
            break
        default:
            let cell = tableView.dequeueReusableCellWithIdentifier("",
                forIndexPath: indexPath) as! UITableViewCell
            selectedStatus = ""
            break

        }

        let task = self.organizedTasks.items[selectedStatus]![indexPath.row]
        cell?.textLabel?.text = task.titolo
        cell?.detailTextLabel?.text = task.priorita
        return cell!

}

我不知道我在想什么...您能幫忙嗎?

首先,這看起來像是使用表視圖的一種方法的災難。 其次,屏幕快照中的第一個標識符是OpenTask(單數),但是您的代碼正在尋找OpenTasks(復數)。

希望這可以幫助

通過對每個cell變量使用let ,您在代碼中出錯。

這是正確的方法:

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

    var cell:UITableViewCell?
    var selectedStatus:String

    switch indexPath.section {
    case 0:
        cell = tableView.dequeueReusableCellWithIdentifier("OpenTasks",
            forIndexPath: indexPath) as! UITableViewCell
        selectedStatus = "Aperti"
        break
    case 1:
        cell = tableView.dequeueReusableCellWithIdentifier("ClosedTasks",
            forIndexPath: indexPath) as! UITableViewCell
        selectedStatus = "Chiusi"
        break
    case 2:
        cell = tableView.dequeueReusableCellWithIdentifier("ExpiredTasks",
            forIndexPath: indexPath) as! UITableViewCell
        selectedStatus = "Scaduti"
        break
    case 3:
        cell = tableView.dequeueReusableCellWithIdentifier("SuspendedTasks",
            forIndexPath: indexPath) as! UITableViewCell
        selectedStatus = "Sospesi"
        break
    default:
        cell = tableView.dequeueReusableCellWithIdentifier("",
            forIndexPath: indexPath) as! UITableViewCell
        selectedStatus = ""
        break

    }

    let task = self.organizedTasks.items[selectedStatus]![indexPath.row]
    cell?.textLabel?.text = task.titolo
    cell?.detailTextLabel?.text = task.priorita
    return cell!

}

暫無
暫無

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

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