简体   繁体   English

带有Swift3的ios10中的UITableView自定义单元重叠

[英]UITableView Custom Cell Overlapping in ios10 with Swift3

I am making UITableView custom cell because height also changed per cell. 我正在制作UITableView自定义单元格,因为每个单元格的高度也发生了变化。

This is my code for initialize cell and after that i want to add UITextView as Subview. 这是我用于初始化单元格的代码,之后我想将UITextView添加为Subview。

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
}

You can see output below. 您可以在下面看到输出。

在此处输入图片说明

It seems that the height calculation in your heightForRowAtIndexPath is not proper. 看来您的heightForRowAtIndexPath中的高度计算不正确。 Consider using self-sizing cells using UITableViewAutomaticDimension and Autolayout to solve your issues. 考虑使用通过UITableViewAutomaticDimension和Autolayout调整大小的单元格来解决您的问题。 Here is a great tutorial that can help you to get started. 是一个很棒的教程,可以帮助您入门。 One more suggestion if you are using UITextView or subclass of the same in a cell and you want it to take the height of the content set its scrollable property to false. 如果在单元格中使用UITextView或相同类的子类,并且希望它占用内容的高度,则将其可滚动属性设置为false,这是另一个建议。

在viewDidLoad中声明此内容,希望对您有所帮助

tableView.estimatedRowHeight = 44

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM