UITextView InputView UITableViewController
UITextView InputView UITableViewController
我正在尝试实现一个 TableView 来覆盖 UITextViews InputView,以便为用户提供一个可搜索的 TableView,然后用选定的值填充 UITextField。
我搜索了一些,我找到的最接近的解决方案是 this,但我不确定如何将 UITableViewController 关联到我的 UITextViews InputView?
我可以看到在 MultiSelectPicker 中调用了这些函数...
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
但这些不是,我相当确定这解释了为什么我在 table 视图中看不到任何内容。
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
}
}
我正在尝试实现一个 TableView 来覆盖 UITextViews InputView,以便为用户提供一个可搜索的 TableView,然后用选定的值填充 UITextField。
我搜索了一些,我找到的最接近的解决方案是 this,但我不确定如何将 UITableViewController 关联到我的 UITextViews InputView?
我可以看到在 MultiSelectPicker 中调用了这些函数...
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
但这些不是,我相当确定这解释了为什么我在 table 视图中看不到任何内容。
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
}
}