IOS10 - 在自定义inputView中使用CollectionView

时间:2016-10-06 07:55:00

标签: ios xcode uicollectionview swift3

我试图输入一个自定义密码,数字从0到9,一些空键随机放置,如下所示:

enter image description here

这是我的简单UICollectionViewCell

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

和我持有UICollectionView的UIView(作为测试我只尝试使用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

    }
}

我当前所做的事情并没有导致我的应用程序崩溃,视图显示为应该和代理被调用,但是我应该丢失我点击的单元格的引用。

我在创建单元格期间从调试器获得了一些日志:

enter image description here

点击第一个单元格后:

enter image description here

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

1 个答案:

答案 0 :(得分:1)

相反,在选择委托方法中将单元格出列,请尝试这种方式来获取单元格。

    Bundle params = new Bundle();
    params.putString("source", AssetsUtils.getExportMoviePath(this)); ///data/user/0/com.bundlecomp.appname/files/export.mp4
    params.putString("name", "TestName");
    params.putString("title", "TestTitle");
    params.putString("filename", "export.mp4");
    params.putString("description", "Created with http://wwww.test.tu");
    Log.d(TAG, "Posting on user wall");
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "me/videos",
            params,
            HttpMethod.POST,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    Log.d(TAG, "GOT response from facebook. Error : " + response.getRawResponse());
                    Log.d(TAG, "GOT response from facebook. Error : " + response.getError());
                    Log.d(TAG, "GOT response from facebook. Resp : " + response);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                            Log.d(TAG, "Posted");

                        }
                    });
                }
            }
    ).executeAsync();

希望这有帮助