在tableview中使用两个不同的单元格

时间:2016-03-30 07:31:55

标签: ios xcode swift mapkit

我想在tableview中显示两个不同的地图,我之前使用过此代码,但现在Xcode抱怨该单元格没有成员nameLabeladdressLabel。我错过了什么吗?

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

    var cell: UITableViewCell!

    if indexPath == 0 {

        cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell

        cell.nameLabel.text = ""
        cell.addressLabel.text = ""
        cell.cityLabel.text = ""

        let inspectionDate: String = detailItem!["dato"] as! String
        cell.inspectionDateLabel.text = self.convertDateString(inspectionDate)

    }
    else
    {
        cell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell

        // Set map options
    }

    return cell
}

2 个答案:

答案 0 :(得分:3)

做类似的事情:

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

            if indexPath == 0 {

               let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell

                cell.nameLabel.text = ""
                cell.addressLabel.text = ""
                cell.cityLabel.text = ""

                let inspectionDate: String = detailItem!["dato"] as! String
                cell.inspectionDateLabel.text = self.convertDateString(inspectionDate)
                return cell

            }
            else
            {
               let cell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell

                // Set map options
             return cell
            }


        }

答案 1 :(得分:2)

您的问题是您输入的cellUITableViewCell,然后尝试将其用作您已出列的特定类型。你应该这样做:

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

    let cell: UITableViewCell

    if indexPath == 0 {

        let summaryCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell

        summaryCell.nameLabel.text = ""
        summaryCell.addressLabel.text = ""
        summaryCell.cityLabel.text = ""

        let inspectionDate: String = detailItem!["dato"] as! String
        summaryCell.inspectionDateLabel.text = self.convertDateString(inspectionDate)

        cell = summaryCell
    }
    else
    {
        let mapCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell

        // Set map options
        cell = mapCell
    }

    return cell
}

这里我将单元格作为特定类型出列,然后在返回之前将cell设置为指向它。