gpt4 book ai didi

swift - UIColletionViewCell 中的 UITextField 成为 FirstResponder

转载 作者:行者123 更新时间:2023-11-28 13:57:29 26 4
gpt4 key购买 nike

我知道这个问题可能有很多关于 SO 的答案。但是在尝试了在互联网上找到的所有解决方案之后(加上我的一些自定义发明..)我仍然无法实现我想要实现的目标。

故事是这样的:

我有一个 UICollectionViewCell,其中嵌入了一个 UITextField 的子类。

这是我的子类:

class CustomTextField: UITextField {

private let padding = UIEdgeInsets(top: 6.0, left: 0.0, bottom: 0.0, right: 2.0)

private lazy var lineView: UIView = {
let lineView = UIView()
lineView.translatesAutoresizingMaskIntoConstraints = false
lineView.isUserInteractionEnabled = false
lineView.frame.size.height = 2
lineView.backgroundColor = UIColor.tiara
return lineView
}()

override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.inset(by: padding)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}

func toggleColors() {
if isFirstResponder {
lineView.backgroundColor = .black
} else {
lineView.backgroundColor = UIColor.tiara
}
}
}

private extension CustomTextField {
func commonInit() {
addSubview(lineView)
constraintLineView()
textColor = UIColor.tiara
}

func constraintLineView() {
lineView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
lineView.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
lineView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
lineView.heightAnchor.constraint(equalToConstant: 2.0).isActive = true
}
}

这是我在 UICollectionViewCell 中使用的代码:

@discardableResult
func setFirstResponder() -> Bool {
return customTextField.becomeFirstResponder()
}

func endEditing() {
customTextField.resignFirstResponder()
}

customTextField.becomeFirstResponder 的结果总是false

它是从我的 UIViewController 调用的:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard indexPath.row != 0 else { return }

dispatchService.stop()
let topIndexPath = IndexPath(item: 0, section: 0)

let topCell: Cell = collectionView.dequeueReusableCell(for: topIndexPath)
topCell.endEditing()
service.data.rearrange(from: indexPath.row, to: 0)
update()

collectionView.performBatchUpdates({
collectionView.moveItem(at: indexPath, to: topIndexPath)
collectionView.scrollToItem(at: topIndexPath, at: .top, animated: true)
}) { (_) in
let secondCell: Cell = collectionView.dequeueReusableCell(for: topIndexPath)
secondCell.setFirstResponder()
self.dispatchService.reset()
}
}

我真的不知道从哪里开始,这是我提供的最后一个解决方案,它仍然没有显示任何键盘。

我正在使用真实设备 iPhone X iOS 12.1。

最佳答案

我可能没有正确的答案,但我认为问题出在您将单元格放入 collectionView(didSelectItemAt:) 中的方式。

您正在使用 dequeueReusableCell 而不是使用 cellForItem(at:) 来获取您的单元格。因此,您正在创建一个可重复使用的单元格,但没有得到您感兴趣的单元格。

关于swift - UIColletionViewCell 中的 UITextField 成为 FirstResponder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53783540/

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