UITextView InputView UITableViewController [英] UITextView InputView UITableViewController

查看:21
本文介绍了UITextView InputView UITableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个覆盖 UITextViews InputView 的 TableView,为用户提供一个可搜索的 TableView,然后用选定的值填充 UITextField.

I'm trying to implement a TableView that overrides a UITextViews InputView to provide the user with a searchable TableView which then populates the UITextField with the selected value.

我搜索了一些,我找到的最接近的解决方案是 this 但我不确定如何将 UITableViewController 关联到我的 UITextViews InputView?

I've search about a fair bit and the closest solution I have found is this but I'm not sure how to associate the UITableViewController to my UITextViews InputView?

我可以看到在 MultiSelectPicker 中正在调用这些函数...

I can see that these functions are being called in the MultiSelectPicker...

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

override func numberOfSectionsInTableView(tableView: UITableView) -> Int

但这些不是我相当肯定的解释为什么我在表格视图中没有看到任何填充.

But these aren't which I'm fairly sure explains why I'm not seeing anything populated in the table view.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 

我已经更改了 didSet,所以我知道正在从主线程调用 reloadData,但仍然没有.

I have changed the didSet so I know reloadData is being called from the main thread and still nothing.

var items = [NSObject](){
    didSet{
        dispatch_async(dispatch_get_main_queue(),{

            self.tableView.reloadData()

        });
    }
}

这可能与我在主视图控制器中设置 inputView 的方式有关,但我对 iOS 和 Swift 还很陌生,所以我可能缺少一些基本的东西.

Its probably something to do with the way I'm setting the inputView in my main viewcontroller but I'm fairly new to iOS and Swift so I'm probably missing something basic.

    @IBOutlet weak var mspEmployee: MultiSelectPicker!

    let employeePickerView = MultiSelectPicker()
    employeePickerView.items = self.oEmployees
    self.mspEmployee = employeePickerView
    self.txtEmployee.inputView = self.mspEmployee.view

这真的让我发疯,所以任何帮助将不胜感激!

This really is driving me mad so any help would be appreciated!

推荐答案

您需要继承 UITextField 并将 inputView 设置为您的 UITableView 的视图.这是我最近所做的一些类似事情的一些实施.

You need to subclass the UITextField and set the inputView to the view of your UITableView. Here is some of the implentation of something similar I did recently.

class BaseTextField: UITextField{

    //Base textfield so that all custom textfields inherit the disabling of certain uitextfield things
    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        //disabling menu, autocorrection and copy paste functionality
        autocorrectionType = UITextAutocorrectionType.No
        inputAssistantItem.leadingBarButtonGroups = []
        inputAssistantItem.trailingBarButtonGroups = []
    }

    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in
            UIMenuController.sharedMenuController().setMenuVisible(false, animated: false)
        }
        return false
    }
}


class OptionSelectText: BaseTextField{
    let picker:OptionPicker //this is the UITableViewController I Use for selecting values
    required init?(coder aDecoder: NSCoder) {
        picker = OptionPicker()
        picker.view.translatesAutoresizingMaskIntoConstraints = false
        super.init(coder: aDecoder)

        picker.delegate = self
        let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: picker.view.frame.width, height: 44))

        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(OptionSelectText.pickerViewDoneAction))
        let flex = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
        let clearButton = UIBarButtonItem(title: "Clear", style: UIBarButtonItemStyle.Plain, target:self, action: #selector(OptionSelectText.pickerClearAction))
        toolbar.setItems([clearButton, flex, doneButton], animated: false)
        toolbar.userInteractionEnabled = true
        inputView = picker.view
        inputAccessoryView = toolbar
    }
}

这篇关于UITextView InputView UITableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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