簡體   English   中英

使用動態原型單元多次實現自定義uitableviewcell

[英]Multiple implementation of custom uitableviewcell using dynamic prototype cells

在我的項目中使用了三種不同的動態原型tableviewcell。

兩個是備用單元(基本具有不同的選項),一個是自定義單元。 而且,這可能會根據需要進行更改。

我面臨的問題是出隊時。

如果它們通常作為UITableViewCells完成,則無法使用自定義單元格出口或操作。

let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)

而且,如果以其他方式完成操作,則該應用程序將崩潰並顯示以下錯誤。

let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell

錯誤:

Could not cast value of type 'UITableViewCell' (0x38595c58) to 'AppName.SwitchTableViewCell' (0x13cdf8).

下面是cellForRowAtIndexPath方法的實現,

// MARK: - UITableViewDelegate Methods

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Setup a cellIdentifer string to store the cell reuse identifier you want to use for each row.
    var cellIdentifier: String

    // Switch through each row and set the appropriate cell reuse identifier
    switch sections[indexPath.section].items[indexPath.row] {
    case .AudioDevices, .Acknowledgments:
        cellIdentifier = "DisclosureCell"
    case .AllowNotifications, .ShowCloudMusic:
        cellIdentifier = "SwitchCell"
    case .Version:
        cellIdentifier = "RightDetailCell"
    }

    // Populate your cell reuse identifier into the cell

    if cellIdentifier == "SwitchCell" {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
        return cell
    }

    // Switch through each cell, and implement the labels/setup for each row
    // The order of the cases is irrelevant!
    switch sections[indexPath.section].items[indexPath.row] {
    case .AudioDevices:
        cell.textLabel?.text = "Audio Devices"
        cell.detailTextLabel?.text = String(userDefaults.integerForKey(audioDeviceListKey))
    case .AllowNotifications:
        cell.switchCellLabel?.text = "Allow Notifications"
        cell.userInteractionEnabled = false
    case .ShowCloudMusic:
        cell.switchCellLabel?.text = "Show Cloud Music"
    case .Acknowledgments:
        cell.textLabel?.text = "Acknowledgements"
        cell.detailTextLabel?.text = ""
    case .Version:
        cell.textLabel?.text = "Version"
        cell.detailTextLabel?.text = buildVersion
        cell.detailTextLabel?.textColor = UIColor.lightGrayColor()
        cell.userInteractionEnabled = false
    }
    // Return the cell
    return cell
}

屏幕截圖:

圖片

使用默認分隔符。

要在“表視圖”中使用多種單元格,必須放置一些條件以檢查哪個單元格用於特定條件。

func tableView cellForRowAtInd....... {
    //depends on what base you separate both cells 
    if condition = true { 
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
       return cell
    }

}

根據您的cellForRowAtIndexPath代碼進行更新:

您已經在單元的另一部分運行之前返回到單元格之前。 因此,該單元格已經返回,並且對於其他代碼部分沒有用處。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Setup a cellIdentifer string to store the cell reuse identifier you want to use for each row.
    var cellIdentifier: String

    // Switch through each row and set the appropriate cell reuse identifier
    switch sections[indexPath.section].items[indexPath.row] {
    case .AudioDevices, .Acknowledgments:
        cellIdentifier = "DisclosureCell"
    case .AllowNotifications, .ShowCloudMusic:
        cellIdentifier = "SwitchCell"
    case .Version:
        cellIdentifier = "RightDetailCell"
    }

    // Populate your cell reuse identifier into the cell

    if cellIdentifier == "SwitchCell" {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell

        // Switch through each cell, and implement the labels/setup for each row
        // The order of the cases is irrelevant!
        switch sections[indexPath.section].items[indexPath.row] {
        case .AudioDevices:
            cell.textLabel?.text = "Audio Devices"
            cell.detailTextLabel?.text = String(userDefaults.integerForKey(audioDeviceListKey))
        case .AllowNotifications:
            cell.switchCellLabel?.text = "Allow Notifications"
            cell.userInteractionEnabled = false
        case .ShowCloudMusic:
            cell.switchCellLabel?.text = "Show Cloud Music"
        case .Acknowledgments:
            cell.textLabel?.text = "Acknowledgements"
            cell.detailTextLabel?.text = ""
        case .Version:
            cell.textLabel?.text = "Version"
            cell.detailTextLabel?.text = buildVersion
            cell.detailTextLabel?.textColor = UIColor.lightGrayColor()
            cell.userInteractionEnabled = false
        }
        // Return the cell
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

        // Switch through each cell, and implement the labels/setup for each row
        // The order of the cases is irrelevant!
        switch sections[indexPath.section].items[indexPath.row] {
        case .AudioDevices:
            cell.textLabel?.text = "Audio Devices"
            cell.detailTextLabel?.text = String(userDefaults.integerForKey(audioDeviceListKey))
        case .AllowNotifications:
            cell.switchCellLabel?.text = "Allow Notifications"
            cell.userInteractionEnabled = false
        case .ShowCloudMusic:
            cell.switchCellLabel?.text = "Show Cloud Music"
        case .Acknowledgments:
            cell.textLabel?.text = "Acknowledgements"
            cell.detailTextLabel?.text = ""
        case .Version:
            cell.textLabel?.text = "Version"
            cell.detailTextLabel?.text = buildVersion
            cell.detailTextLabel?.textColor = UIColor.lightGrayColor()
            cell.userInteractionEnabled = false
        }
        // Return the cell
        return cell
    }
}

請使用這個

   if cellIdentifier == "SwitchCell" {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
        return cell
    }

在未定義出口的單元格中訪問帶有此類標簽的標簽

if let theLabel = self.view.viewWithTag(123) as? UILabel {
        theLabel.text = "some text"
    }

暫無
暫無

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

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