具有水平分页的UICollectionView单元格UI随机消失 [英] UICollectionView cell UI with horizontal paging randomly disappearing

查看:80
本文介绍了具有水平分页的UICollectionView单元格UI随机消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UICollectionView,它一次显示一个单元格,在启用分页的情况下水平滚动。每个单元格都有一些UI(视图,textViews)。



我有三种方式让用户浏览单元格。 (1)用户点击UIButtons,使用collectionView.scrollToItem在单元格之间进行左右导航;(2)用户通过点击另一个UIButton在数组末尾创建一个新单元格;(3)用户使用scrollViewWillEndDragging滚动。



问题是,在情况(1)和(2)中,单元格UI有时会消失。从一个单元来回导航到另一个单元将使其再次返回。这似乎是随机的。



我有两种方法可以隔离问题,但对于如何找到修复方法仍然一无所知:(a)UI如果我将每个导航之间的所有textViews都完全设置为endEditing(true),它不会消失(这意味着键盘完全消失,然后再次出现)。 (b)如果用户仅使用滚动方法在一个单元格之间导航,它永远不会消失-一旦使用了任何按钮,UI就会再次消失。



<我已经尝试通过使用prepareForReuse()方法在自定义UICollectionView单元中重置UI,但这似乎无济于事。



可能与另一个问题我之前已经问过。



感谢任何指针!!



这是我将单元出队的方式:

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

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

这是我的滚动方法:

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

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

setNewFirstResponder()
}

按钮方法之一:

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

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

collectionView .scrollToItem(at:newIndexPath,at:.left,animation:false)
cur rentCardIndex = autoSaveCards.count-1

DispatchQueue.main.asyncAfter(最后期限:.now()+ 0.5){
self.setNewFirstResponder()
}
}


解决方案

尝试一下。保留一个数组,用于存储在控制器中创建的单元格(例如 cellsArray )。然后,如果元素位于该 indexPath.row ,则从该数组返回。

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

让addCardCell = collectionView.dequeueReusableCell(withReuseIdentifier: CardCell,为:indexPath)为! AddCardCell

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


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()
    }
}

解决方案

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
}

这篇关于具有水平分页的UICollectionView单元格UI随机消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆