奇怪的UITableView行为

时间:2016-03-30 16:39:35

标签: ios xcode swift uitableview storyboard

我在UITableView中遇到一个奇怪的错误。我有两个单元格,SummaryHeaderTableViewCellMapTableViewCell。它们的高度分别为400和200像素。我有两行,第一行应该是摘要单元格,第二行应该是map cell。

但是,模拟器中的视图显示如下:

enter image description here

以下是代码:

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 = detailItem!["navn"] as? String
        summaryCell.addressLabel.text = "\(detailItem!["adrlinje1"]), \(detailItem!["adrlinje2"])"
        summaryCell.cityLabel.text = detailItem!["poststed"] as? String

        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
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if (indexPath.section == 0) {
        return 400.0
    } else {
        return 200.0
    }
}

这是带有动态原型的故事板:

enter image description here

2 个答案:

答案 0 :(得分:2)

你应该引用行而不是heightForRowAtIndexPath中的部分:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if (indexPath.row == 0) {
        return 400.0
    } else {
        return 200.0
    }
}

答案 1 :(得分:1)

section中只有一个UITableView,并且不同的项目填充在两个不同的行上,而不是部分。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let summaryCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell
        summaryCell.nameLabel.text = detailItem!["navn"] as? String
        summaryCell.addressLabel.text = "\(detailItem!["adrlinje1"]), \(detailItem!["adrlinje2"])"
        summaryCell.cityLabel.text = detailItem!["poststed"] as? String
        let inspectionDate: String = detailItem!["dato"] as! String
        summaryCell.inspectionDateLabel.text = self.convertDateString(inspectionDate)
        return summaryCell
    }
    else
    {
        let mapCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell
        // Set map options
        return mapCell
    }
}

另外

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if (indexPath.row == 0) {
         return 400.0
    }
    else {
         return 200.0
    }
}