UITextView InputView UITableViewController

时间:2016-04-19 15:00:01

标签: ios swift uitableview uitextview

我正在尝试实现一个覆盖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()

        });
    }
}

它可能与我在主视图控制器中设置inputView的方式有关,但我对iOS和Swift相当新,所以我可能会遗漏一些基本的东西。

    @IBOutlet weak var mspEmployee: MultiSelectPicker!

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

这真让我生气,所以任何帮助都会受到赞赏!

1 个答案:

答案 0 :(得分:0)

您需要将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
    }
}