gpt4 book ai didi

ios - UICollectionView 单元格 UI 水平分页随机消失

转载 作者:行者123 更新时间:2023-11-30 11:26:11 25 4
gpt4 key购买 nike

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

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

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

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

我尝试使用prepareForReuse()方法重置自定义UICollectionView单元中的UI,但这似乎没有帮助。

这可能与 another question 有关我之前问过。

谢谢各位指点!!

这就是我使单元出队的方式:

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
}

这是我的滚动方法:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: 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, animated: false)
currentCardIndex = autoSaveCards.count - 1

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

最佳答案

试试这个。保留一个数组来存储您在 Controller 中创建的单元格(例如cellsArray)。如果该 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
}

关于ios - UICollectionView 单元格 UI 水平分页随机消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50715980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com