0

当单击单元格时,我无法使用 didHighlightItem 和 didUnhighlightItem 函数正确设置动画,因为我无法访问单元格的节号。我想知道我是否错误地解决了问题,或者是否有办法在集合视图中访问 SectionController 的部分索引。我的代码如下:

class PersonSectionController: ListSectionController {
    var current: Person?

    override init() {}

    override func didUpdate(to object: Any) {
        if let person = object as? Person {
            current = person
        }
    }

    // Number of items displayed per object
    override func numberOfItems() -> Int {
        return 1
    }

    // Dequeue and configure cell
    override func cellForItem(at index: Int) -> UICollectionViewCell {}

    // Returns the required cell size at the given index
    override func sizeForItem(at index: Int) -> CGSize {}

    override func didHighlightItem(at index: Int) {
        if let viewController = self.viewController as? PeopleViewController {
            if let cell = viewController.peopleCollectionView.cellForItem(at: IndexPath(row: index, section: 0)) {
                UIView.animate(withDuration: 0.1) {
                    cell.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
                }
            }
        }
    }

    override func didUnhighlightItem(at index: Int) {
        if let viewController = self.viewController as? PeopleViewController {
            if let cell = viewController.peopleCollectionView.cellForItem(at: IndexPath(row: 0, section: index)) {
                UIView.animate(withDuration: 0.1) {
                    cell.transform = CGAffineTransform.identity
                }
            }
        }
    }
}

提前致谢!

4

1 回答 1

0

我认为您需要从以下位置访问单元格collectionContext

override func didHighlightItem(at index: Int) {
    guard let cell = collectionContext?.cellForItem(at: index) else { return }        
    // Animation stuff
}
于 2019-03-12T00:30:37.197 回答