UICollectionView cell UI with horizontal paging randomly disappearing

拜拜、爱过 提交于 2019-12-11 15:19:14

问题


I have a UICollectionView which shows one cell at a time, scrolls horizontally with paging enabled. Each cell has some UI (views, textViews).

I have three ways how the user can navigate through cells. (1) User taps UIButtons to navigate left/right from cell to cell using collectionView.scrollToItem, (2) user creates a new cell at the end of array by tapping another UIButton, (3) user scrolls using scrollViewWillEndDragging.

The problem is, that in cases (1) and (2), occasionally the cell UI disappears. Navigating back and forth from one cell to another will bring it back again. This appears to be random.

There are two ways how I was able to isolate the problem, but still am clueless as how to find a fix: (a) The UI does not disappear if I entirely set all textViews to endEditing(true) between each navigation (which means the keyboard fully disappears before re-appearing again). (b) it never disappears if the user only uses the scroll method to navigate from cell to cell - as soon as any of the buttons are used, the UI will disappear again.

I have tried to reset the UI in the custom UICollectionView cell by using the prepareForReuse() method, but that didn't seem to help.

Possibly this is related to another question I have asked earlier.

Thanks for any pointers!!

This is how I am dequeuing my cell:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let addCardCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as! AddCardCell

    addCardCell.autoSaveCard = autoSaveCards[indexPath.item]
    return addCardCell
}

Here is my scroll method:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    autoSaveCard()

    let x = targetContentOffset.pointee.x
    currentCardIndex = Int(x / view.frame.width)
    updateKeyboardInputLabelsAndButtons()

    setNewFirstResponder()
}

One of the button methods:

@objc func handleAddCell() {
    autoSaveCard()
    createEmptyAutoSaveCard()

    let newIndexPath = IndexPath(item: autoSaveCards.count - 1, section: 0)
    collectionView.insertItems(at: [newIndexPath])

    collectionView.scrollToItem(at: newIndexPath, at: .left, animated: false)
    currentCardIndex = autoSaveCards.count - 1

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
        self.setNewFirstResponder()
    }
}

回答1:


Try this. Keep an array for storing the cells you create in the controller (say cellsArray). Then return from this array if element exists at that indexPath.row.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if cellsArray[indexPath.row] != nil {
        return cellsArray[indexPath.row]
    }

    let addCardCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as! AddCardCell

    addCardCell.autoSaveCard = autoSaveCards[indexPath.item]
    cellsArray.append(addCardCell)
    return addCardCell
}


来源:https://stackoverflow.com/questions/50715980/uicollectionview-cell-ui-with-horizontal-paging-randomly-disappearing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!