本文介绍了在UITableView中滚动到顶部后,CAGradientLayer的位置错误。迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在特定的 UITableViewCell 的底部阴影。我做到了,但是当我滚动到底部时,它工作正常,当我滚动到顶部时,阴影位置错误,它出现在 UITableViewCell 的顶部。我尝试了几种方法,但对我不起作用。我阅读了这个和这个如何解决?

I need to make a shadow on the bottom on the specific UITableViewCell. I made it but when I scroll to bottom it works fine, when I scroll to top the shadow has wrong position and it appears on the top of UITableViewCell. I tried several methods but it doesn't work for me. I read this question and this question How can I fix it?

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier, forIndexPath: indexPath) as! LifelineLeaderboardTableViewCell

        // Configure the cell...
        let lifelineRecentModel = users[indexPath.row]

        cell.clipsToBounds = false
        if let currentUserID = DBHelper.instance.mainUserId {
            if lifelineRecentModel.user.id == currentUserID {
                cell.setupUserNumberLabelTextColor(true)
                cell.showBlueLineView(true)
//                cell.showShadow(true)
                let shadowView = UIView(frame: cell.bounds)

                let shadowFrame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: cell.bounds.width, height: 90))
                let shadowPath = UIBezierPath(rect: shadowFrame).CGPath
                let shadow = CAGradientLayer()

                shadow.shadowOpacity = 0.25
                shadow.shadowColor = UIColor.blackColor().CGColor
                shadow.shadowPath = shadowPath
                shadowView.layer.insertSublayer(shadow, atIndex: 0)
                cell.contentView.addSubview(shadowView)
            } else {
                cell.setupUserNumberLabelTextColor(false)
                cell.showBlueLineView(false)
//                cell.showShadow(false)
            }
        } else {
            cell.setupUserNumberLabelTextColor(false)
            cell.showBlueLineView(false)
//            cell.showShadow(false)
        } 

        return cell
    }

我也尝试了以下函数

  func showShadow(bool: Bool) {
    let shadowFrame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: bounds.width, height: bounds.height + 10))
    let shadowPath = UIBezierPath(rect: shadowFrame).CGPath

    layer.shadowOpacity = 0.25
    layer.shadowColor = UIColor.blackColor().CGColor
    layer.shadowPath = shadowPath
    clipsToBounds = !bool
}

我也尝试过

        cell.clipsToBounds = false
        if let currentUserID = DBHelper.instance.mainUserId {
            if lifelineRecentModel.user.id == currentUserID {
                cell.setupUserNumberLabelTextColor(true)
                cell.showBlueLineView(true)
//                cell.showShadow(true)
                cell.layer.shadowPath = UIBezierPath(rect: cell.bounds).CGPath
                cell.layer.shadowOpacity = 0.5
                cell.layer.shadowOffset = CGSize(width: 0, height: 10)

            } else {
                cell.setupUserNumberLabelTextColor(false)
                cell.showBlueLineView(false)
//                cell.showShadow(false)
                cell.layer.shadowOpacity = 0

            }
        } else {
            cell.setupUserNumberLabelTextColor(false)
            cell.showBlueLineView(false)
//            cell.showShadow(false)
            cell.layer.shadowOpacity = 0
        }


推荐答案

我认为您应该从不应包含阴影的单元格中去除阴影。像这样

I think you should "remove" shadow from cells which mustn't contain shadow. Like this

if let currentUserID = DBHelper.instance.mainUserId {
  ..........
} else { 
   .......
   shadow.shadowColor = UIColor.clearColor().CGColor
}

可以添加屏幕截图坏单元格吗?

Can you add screenshots 'bad' cells?

这篇关于在UITableView中滚动到顶部后,CAGradientLayer的位置错误。迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:34