【问题标题】:UITableView Custom Cell Overlapping in ios10 with Swift3ios10中的UITableView自定义单元格与Swift3重叠
【发布时间】:2017-02-28 17:13:09
【问题描述】:

我正在制作 UITableView 自定义单元格,因为每个单元格的高度也发生了变化。

这是我初始化单元格的代码,之后我想将 UITextView 添加为子视图。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    let dictAd = self.arrAllQuestions[indexPath.row] as! NSDictionary

    let fontNew = UIFont(name: "AvenirNext-Regular", size: 19.0)
    let strA = "\(indexPath.row+1). \(dictAd.object(forKey: "Title")!)"

    let heightTitle = self.heightForView(text: strA, font: fontNew!, width: tableView.frame.size.width-16)

    return heightTitle+5;
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let dictAd = self.arrAllQuestions[indexPath.row] as! NSDictionary

    let cell = tableView.dequeueReusableCell(withIdentifier: "quesionCell", for: indexPath) as! QuestionCell

    let fontNew = UIFont(name: "AvenirNext-Regular", size: 19.0)

    let strA = "\(indexPath.row+1). \(dictAd.object(forKey: "Title")!)"

    cell.lblName.font = fontNew
    cell.lblName.text = strA
    cell.lblName.numberOfLines = 0
    let heightTitle = self.heightForView(text: strA, font: fontNew!, width: tableView.frame.size.width-16)

    var frame = cell.lblName.frame as CGRect
    frame.size.height = heightTitle; //you need to adjust this value
    cell.lblName.frame = frame;


    let txtView = AnimatableTextView(frame: CGRect(x: 8, y: cell.lblName.frame.origin.y+cell.lblName.frame.size.height+5, width: tableView.frame.size.width-16, height: 25))
    txtView.borderWidth = 1.0
    txtView.borderColor = UIColor.lightGray
    txtView.cornerRadius = 4.0
    cell.contentView.addSubview(txtView)

    return cell
}

您可以在下面看到输出。

【问题讨论】:

  • 最好改进一下关于什么是正确结果以及quesionCell 的代码是什么的问题。我的猜测是您没有在委托方法中为每个单元格设置正确的高度。
  • 你在执行heightForRowAt吗?
  • 从情节提要中添加标签和文本视图,并通过情节提要应用约束

标签: ios uitableview swift3 xcode8 ios10


【解决方案1】:

您的 heightForRowAtIndexPath 中的高度计算似乎不正确。考虑使用使用 UITableViewAutomaticDimension 和 Autolayout 的自调整单元格来解决您的问题。 Here 是一个很棒的教程,可以帮助您入门。如果您在单元格中使用 UITextView 或相同的子类,并且您希望它采用内容的高度,请将其可滚动属性设置为 false,那么还有一个建议。

【讨论】:

  • 如果您使用自动布局,UITableViewAutomaticDimension 是最佳答案。
  • 然后正如我所说,检查你在 heightForRowAtIndexPath 中的高度计算。
  • 如果我为 ex 设置静态高度,我什至检查了同样的问题。返回 500。
  • 当您的单元格有更多视图组件时,您正在使用一种方法来设置单元格和标签 self.heightForView 的高度。所以你应该在 let heightTitle = self.heightForView(text: strA, font: fontNew!, width: tableView.frame.size.width-16) 之后添加所有其他视图的高度
【解决方案2】:

在 viewDidLoad 中声明 希望对你有帮助

tableView.estimatedRowHeight = 44

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 2011-02-23
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多