gpt4 book ai didi

IOS10 - 在自定义 inputView 中使用 CollectionView

转载 作者:行者123 更新时间:2023-11-29 00:39:15 24 4
gpt4 key购买 nike

我正在尝试使用 0 到 9 的数字输入自定义密码,并像这样随机放置一些空键:

enter image description here

这是我的简单 UICollectionViewCell

class KeyboardKeyCollectionViewCell: UICollectionViewCell {
@IBOutlet var mLabel: UILabel!
}

我的 UIView 包含 UICollectionView(作为测试,我只尝试使用 3 个单元格)

import UIKit
protocol KeyboardDelegate: class {
func keyWasTapped(character: String)
}

class PasswordKeyboard: UIView , UICollectionViewDataSource , UICollectionViewDelegate {

@IBOutlet var mCollectionView: UICollectionView!
weak var delegate: KeyboardDelegate?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initializeSubviews()
}

override init(frame: CGRect) {
super.init(frame: frame)
initializeSubviews()
}

func initializeSubviews() {
let lBundle = Bundle(for: type(of: self))
let lKeyboardNib = UINib(nibName: "PasswordKeyboard", bundle: lBundle)
let lCellNib = UINib(nibName: "KeyboardKeyCollectionViewCell", bundle: lBundle)
let lKeyboardNibView = lKeyboardNib.instantiate(withOwner: self, options: nil).first as! UIView
self.addSubview(lKeyboardNibView)
lKeyboardNibView.frame = self.bounds

mCollectionView.dataSource = self
mCollectionView.delegate = self

mCollectionView.register(lCellNib, forCellWithReuseIdentifier: "idgaf")
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "idgaf", for: indexPath as IndexPath) as! KeyboardKeyCollectionViewCell
cell.mLabel.text = String(describing: indexPath.item) //TODO Randomize this
return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "idgaf", for: indexPath as IndexPath) as! KeyboardKeyCollectionViewCell
self.delegate?.keyWasTapped(character: cell.mLabel.text!)

}
}

以及带有将使用该自定义键盘的文本字段的 ViewController

class LoginController: UiViewController  , KeyboardDelegate
{
func keyWasTapped(character: String) {
passwordField.insertText(character)
}

override func viewDidLoad()
{
super.viewDidLoad()
let keyboardView = PasswordKeyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
keyboardView.delegate = self
passwordField.inputView = keyboardView

}
}

我目前所做的并没有使我的应用程序崩溃, View 按应有的方式显示并且委托(delegate)被调用是应该的,但是我丢失了我单击的单元格的引用。

我在创建单元格的过程中从调试器获得了一些日志:

enter image description here

在点击第一个单元格之后:

enter image description here

我一直在尝试解决这个问题,但找不到导致此问题的原因。感谢您阅读到此。

最佳答案

不要在您的选择委托(delegate)方法中使单元格出队,而是尝试以这种方式获取单元格。

func cellForItem(at indexPath: IndexPath) -> UICollectionViewCell?



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let cell = cellForItem(indexPath) as! KeyboardKeyCollectionViewCell
self.delegate?.keyWasTapped(character: cell.mLabel.text!)

}

希望对你有帮助

关于IOS10 - 在自定义 inputView 中使用 CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39890547/

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