使用动态原型单元实现自定义uitableviewcell的多个实现

时间:2016-10-06 04:52:20

标签: ios swift uitableview

我的项目中使用了三种不同的动态原型tableviewcells。

两个是库存单元格(基本具有不同选项),一个是自定义单元格。而且,这可能会根据需要改变。

我面临的问题是在细胞出列时。

如果它们作为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
}

截图:

Image

2 个答案:

答案 0 :(得分:0)

Use Default separator .

要在Table View中使用多种单元格,您必须设置一些条件来检查哪些单元格用于特定条件。

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

答案 1 :(得分:0)

使用此

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

在该单元格中使用这样的标签访问标签,其中yon没有定义出口

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