【问题标题】:Strange UITableView behaviour奇怪的 UITableView 行为
【发布时间】:2016-07-18 19:25:03
【问题描述】:

我在 UITableView 中遇到了一个奇怪的错误。我有两个单元格,SummaryHeaderTableViewCellMapTableViewCell。它们有 400 和 200 像素高。我有两行,第一行应该是汇总单元格,第二行应该是地图单元格。

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

代码如下:

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

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

【问题讨论】:

    标签: ios xcode swift uitableview storyboard


    【解决方案1】:

    您应该在 heightForRowAtIndexPath 中引用行而不是部分:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多