滚动时自定义表格视图单元格绘制错误高度

时间:2015-12-03 11:17:09

标签: ios swift uitableview cell

我正在自定义willDisplayCell方法中的表格视图单元格。出于某种原因,它在滚动时绘制了一些错误高度的子视图(参见视频)。我无法弄清楚为什么......

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    cell.contentView.backgroundColor=UIColor.clearColor()
    cell.reloadInputViews()

    let height = cell.frame.height - 15

    var whiteRoundedCornerView:UIView!
    whiteRoundedCornerView=UIView(frame: CGRectMake(7,10,self.view.bounds.width-14,height))
    whiteRoundedCornerView.backgroundColor=getColorByID(colorID!)
    whiteRoundedCornerView.layer.masksToBounds=false

    whiteRoundedCornerView.layer.shadowOpacity = 1.55;

    whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(1, 0);
    whiteRoundedCornerView.layer.shadowColor=UIColor.grayColor().CGColor

    whiteRoundedCornerView.layer.cornerRadius=5.0
    whiteRoundedCornerView.layer.shadowOffset=CGSizeMake(-1, -1)
    whiteRoundedCornerView.layer.shadowOpacity=0.5

    whiteRoundedCornerView.layer.shouldRasterize = true
    whiteRoundedCornerView.layer.rasterizationScale = UIScreen.mainScreen().scale


    if cell.contentView.subviews.count < 6 {  //  to avoid multible subview adding when scrolling...

        cell.contentView.addSubview(whiteRoundedCornerView)
        cell.contentView.sendSubviewToBack(whiteRoundedCornerView)

    }
}

以下是该问题的视频:

https://vid.me/QeV0

更新

我还尝试将代码移至layoutSubviews,但问题仍然存在......

override func layoutSubviews() {
    super.layoutSubviews()

    let height = self.frame.height - 15

    var whiteRoundedCornerView:UIView!
    whiteRoundedCornerView=UIView(frame: CGRectMake(7,10,UIScreen.mainScreen().bounds.width-14,height))
    whiteRoundedCornerView.backgroundColor=UIColor.greenColor()//getColorByID(colorID!)
    whiteRoundedCornerView.layer.masksToBounds=false

    whiteRoundedCornerView.layer.shadowOpacity = 1.55;

    whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(1, 0);
    whiteRoundedCornerView.layer.shadowColor=UIColor.grayColor().CGColor

    whiteRoundedCornerView.layer.cornerRadius=5.0
    whiteRoundedCornerView.layer.shadowOffset=CGSizeMake(-1, -1)
    whiteRoundedCornerView.layer.shadowOpacity=0.5

    whiteRoundedCornerView.layer.shouldRasterize = true
    whiteRoundedCornerView.layer.rasterizationScale = UIScreen.mainScreen().scale


    if self.contentView.subviews.count < 6 {              

        self.contentView.addSubview(whiteRoundedCornerView)
        self.contentView.sendSubviewToBack(whiteRoundedCornerView)

    }
}

如果我不检查tableview.subviews.count,它会在滚动时为单元格添加大量子视图。

我希望在重复使用单元格时更改现有子视图的高度。

2 个答案:

答案 0 :(得分:1)

您需要检查多次不添加子视图。如果发生这种情况,您之前已经添加了子视图,但它们的高度现在可能是错误的,您必须更新它们。

UITableView重复使用单元格,因此不同高度的新单元格可能已经包含您的子视图,但由于您没有更新它,因此高度错误。

你应该干净地分开子视图的设置和布局,如下所示:

class MyCell: UITableViewCell {

    private let whiteRoundedCornerView = UIView()


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setUp()
    }


    required init?(coder: NSCoder) {
        super.init(coder: coder)

        setUp()
    }


    override func layoutSubviews() {
        super.layoutSubviews()

        let bounds = self.bounds

        var whiteRoundedCornerViewFrame = CGRect()
        whiteRoundedCornerViewFrame.origin.x = 7
        whiteRoundedCornerViewFrame.origin.y = 10
        whiteRoundedCornerViewFrame.size.width = bounds.width - 7 - whiteRoundedCornerViewFrame.origin.x
        whiteRoundedCornerViewFrame.size.height = bounds.height - 5 - whiteRoundedCornerViewFrame.origin.y
        whiteRoundedCornerView.frame = whiteRoundedCornerViewFrame
    }


    private func setUp() {
        setUpWhiteRoundedCornerView()
    }


    private func setUpWhiteRoundedCornerView() {
        let child = whiteRoundedCornerView
        child.backgroundColor = .greenColor()

        let layer = child.layer
        layer.cornerRadius = 5.0
        layer.rasterizationScale = UIScreen.mainScreen().scale
        layer.shadowColor = UIColor.grayColor().CGColor
        layer.shadowOffset = CGSize(width: -1, height: -1)
        layer.shadowOpacity = 0.5
        layer.shouldRasterize = true

        insertSubview(child, atIndex: 0)
    }
}

答案 1 :(得分:0)

如果不同行的数据大小不同,则必须动态设置tableview单元格的高度

以下是代码:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{

    var height:CGFloat

    let constraint1:CGSize = CGSizeMake(self.view.frame.size.width/2-70, 5000.0)
    let string="eefdgvfgfhgfgfhgfgjhidffjo;dkfnkjfldjfkjfkjfldfjkkjhbkjflfjihkfjkfjgfkjfkgfkgjlfgnfjgnfgnfgbkgmfjgfkgjfkggjlfgklkgkgkglkgggfkglgkkgjlgkgk"

    let answerSize = string.boundingRectWithSize(constraint1, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(17.0)], context: nil)
    let questionSize = string.boundingRectWithSize(constraint1, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(17.0)], context: nil)   

    height=answerSize.height + questionSize.height

    return height
}
相关问题