问题描述
在我的 iOS 应用程序中,我创建了一个带有自定义单元格的表格视图控制器,其中一个包含 PickerView.这是该自定义单元格的类:
In my iOS app I have created a table view controller with custom cells where one of them contains a PickerView. Here's the class for that custom cell:
import Foundation
import UIKit
class PickerCell: UITableViewCell {
@IBOutlet weak var label : UILabel!
@IBOutlet weak var pickerView : UIPickerView!
func configurePickerCell(labelText:String, pickerDelegate:MyPickerViewController, enabled:Bool, defaultValueIndex:Int) {
self.label.text = labelText
var labelFrame:CGRect = self.label.frame
labelFrame.size.height = Settings.labelHeight
self.label.frame = labelFrame
self.pickerView.delegate = pickerDelegate
self.pickerView.dataSource = pickerDelegate
if (enabled) {
self.pickerView.userInteractionEnabled = true
}
else {
self.pickerView.userInteractionEnabled = false
self.pickerView.selectRow(defaultValueIndex, inComponent: 0, animated: true)
}
}
}
这里是选择器视图控制器类:
and here is the picker view controller class:
import Foundation
import UIKit
class MyPickerViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
var pickerData:[String]!
init(pickerData: [String]){
super.init(nibName: nil, bundle: nil)
self.pickerData = pickerData
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println(self.pickerData[row])
}
}
并且是我的表视图控制器中的一对方法:
and are a pair of method in my table view controller:
override func viewWillAppear(animated: Bool) {
self.tabBarController?.navigationItem.leftBarButtonItem = nil
self.tabBarController?.navigationItem.setHidesBackButton(false, animated: true)
self.projectService.requestAllProjects {
(response) in
self.projects = self.projectService.loadProjects(response) as! [Project]
//println(self.projects.count)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
self.userService.requestAllUsers {
(response) in
self.users = self.userService.loadUsers(response) as! [User]
//println(self.users.count)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
虽然这是在内部调用
override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
self.projectCell = tableView.dequeueReusableCellWithIdentifier("PickerCell", forIndexPath: indexPath) as! PickerCell
let pickerController = MyPickerViewController(pickerData: self.projectService.getArrayOfNames(self.projects))
self.projectCell.configurePickerCell("Progetto", pickerDelegate: pickerController, enabled: true, defaultValueIndex: 0)
return self.projectCell
...
}
问题是我在标题中看到错误,我不知道这是什么意思.self.projects 是从数据库中检索的 Project 对象数组
The problem is that I get the error in title and I don't know what it means. self.projects is an array of Project objects retrieved from a database with
NSURLSession.sharedSession().dataTaskWithURL()
并且它被正确填充,所以我不知道我在哪里犯了错误.你能帮忙吗?
and it is correctly populated so I don't know where I am making the mistake.Can you help please?
更新非常感谢@NickCatib,这是我的工作代码.注意:不需要更多类作为 Picker 视图委托.我刚刚在自定义单元格中实现了它:
UPDATEMany thanks to @NickCatib, here's my working code.NB: there is no need of a further class as Picker view delegate. I have just implemented it just inside the custom cell:
import Foundation
import UIKit
class PickerCell: UITableViewCell, UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet weak var label : UILabel!
@IBOutlet weak var pickerView : UIPickerView!
var pickerData:[String]!
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println(self.pickerData[row])
}
func configurePickerCell(labelText:String, enabled:Bool, defaultValueIndex:Int) {
self.label.text = labelText
var labelFrame:CGRect = self.label.frame
labelFrame.size.height = Settings.labelHeight
self.label.frame = labelFrame
self.pickerView.delegate = self
self.pickerView.dataSource = self
if (enabled) {
self.pickerView.userInteractionEnabled = true
}
else {
self.pickerView.userInteractionEnabled = false
self.pickerView.selectRow(defaultValueIndex, inComponent: 0, animated: true)
}
}
}
推荐答案
好吧,我会在 tableView
控制器或单元本身中为选择器视图设置委托.基本上我认为最好的方法是立即处理 UIPicker
的这些变化,所以尝试在单元格或 UITableViewController
内实现 delegate
>.由于有可能有多个选择器,我会在这种特殊情况下在单元格内处理它们.
Well I would set delegate for the picker view either in tableView
controller or cell itself. Basically I think that best way to deal with these kind of changes of the UIPicker
is right away, so try to implement delegate
inside the cell or UITableViewController
. Since there is a chance to have multiple pickers, I would deal with them inside the cell for this particular case.
这篇关于[UILabel numberOfComponentsInPickerView:]: 无法识别的选择器发送到实例...]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!