2

I want to show two different maps in a tableview, and I have used this code previously, but now Xcode complains that the cell has no members nameLabel, addressLabel. Am I missing something?

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
}
rebellion
  • 6,628
  • 11
  • 48
  • 79

2 Answers2

3

Do something like that:

 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
            }


        }
Muzahid
  • 5,072
  • 2
  • 24
  • 42
2

Your problem is that you are typing the cell as a UITableViewCell and then trying to use it as the specific type you have dequeued. You should be doing this:

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
}

Here I am dequeuing the cell as a specific type, and then setting the cell to point to it before returning it.

Infinity James
  • 4,667
  • 5
  • 23
  • 36