我正在尝试实现一个重写UITextViews InputView的TableView,以向用户提供可搜索的TableView,然后使用选定的值填充UITextField。

我已经搜索了相当多的内容,找到的最接近的解决方案是this,但是我不确定如何将UITableViewController关联到UITextViews InputView?

我看到这些函数在MultiSelectPicker中被调用...

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

override func numberOfSectionsInTableView(tableView: UITableView) -> Int


但是这些不是我可以确定地解释的原因,为什么我在表格视图中看不到任何内容。

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

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


我已经更改了didSet,所以我知道从主线程调用reloadData还是什么都没有。

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

            self.tableView.reloadData()

        });
    }
}


这可能与我在主viewcontroller中设置inputView的方式有关,但是我对于iOS和Swift还是相当陌生,所以我可能缺少一些基本知识。

    @IBOutlet weak var mspEmployee: MultiSelectPicker!

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


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

最佳答案

您需要子类化UITextField,并将inputView设置为UITableView的视图。这是我最近做过的一些类似事情的实现。

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
    }
}

关于ios - UITextView InputView UITableViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36722185/

10-11 09:19