ios - UITableView 在动画时剪辑到边界

标签 ios swift

我有一个 UITableView,其中一些单元格有阴影。 当我做出平移手势来查看任何单元格的行操作时,表格 View 会剪辑单元格的边界,直到手势完成。

我在单元格的 contentView 及其 super View 以及表格 View 上将 clipsToBounds 设置为 false

我的自定义单元格代码:

var isExpanded: Bool = false

@IBOutlet weak var shadowView: UIView!
@IBOutlet weak var fullContainer: UIView!

override func awakeFromNib() {
    super.awakeFromNib()
    reloadUI()
}

func reloadUI() {
    contentView.superview?.clipsToBounds = false
    contentView.clipsToBounds = false

    fullContainer.layer.masksToBounds = true
    fullContainer.layer.cornerRadius = 8

    shadowView.layer.masksToBounds = false
    shadowView.layer.cornerRadius = 8
    shadowView.layer.shadowOpacity = 1.0
    shadowView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    shadowView.layer.shadowPath = UIBezierPath(roundedRect: fullContainer.bounds, cornerRadius: 8).cgPath
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    isExpanded = selected
    reloadUI()
}

带有表格 View 的 View Controller 的代码:

extension SecondViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let action = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
        return [action]
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.reloadRows(at: [indexPath], with: .automatic)
        selectedIndex = indexPath.section
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section != 0 ? 8 : 1
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.clipsToBounds = false
        return view
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if selectedIndex == indexPath.section {
            return 120
        }
        return 108
    }
}

SecondViewController 也有此代码,因为它有一个平移识别器,用于通过手势在 View Controller 之间进行更改

extension SecondViewController: UIGestureRecognizerDelegate {

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == recognizer || otherGestureRecognizer == recognizer {
            return false
        }
        return true
    }

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == recognizer{
            for cell in tableView.visibleCells {
                let location = gestureRecognizer.location(in: cell.contentView)
                if cell.contentView.layer.contains(location) {
                    return false
                }
            }
            return true
        }
        return true
    }
}

UITableViewCell 已剪切边界:

TableViewCell with bounds clipped

最佳答案

这看起来像是 UITableView 中的一个错误。可以通过以下扩展来解决:

extension UITableView {
    func fixCellBounds() {
        DispatchQueue.main.async { [weak self] in
            for cell in self?.visibleCells ?? [] {
                cell.layer.masksToBounds = false
                cell.contentView.layer.masksToBounds = false
            }
        }
    }
}

只需在调用reloadRows/insertRows/deleteRows等后调用它:

tableView.reloadRows(at: <index paths>, with: .automatic)
tableView.fixCellBounds()

之前:

cell animation before the fix

之后:

cell animation after the fix

关于ios - UITableView 在动画时剪辑到边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45927174/

相关文章:

ios - SQLite3多线程

iOS pdf 加密 256 位 AES

ios - WebKit View 未连接到 ViewController(Control + 拖动)

arrays - Swift:改变结构中的数组

ios - Swift 计算器运算符问题

ios 使用字典对数组进行排序

objective-c - UIView animation block : if duration is 0, 和没有动画一样吗?

arrays - 从数组设置自定义单元格 uibutton 标题 - Swift

swift - CharacterSet和CharacterSet.inverted打印出来的结果是一样的

ios - CGColorGetComponents() 没有为黑色和白色返回正确的值