[UILabel numberOfComponentsInPickerView:]:无法识别的选择器发送到实例...]

时间:2015-09-11 11:39:26

标签: ios swift uitableview uipickerview unrecognized-selector

在我的iOS应用程序中,我创建了一个带有自定义单元格的表格视图控制器,其中一个包含PickerView。这是该自定义单元格的类:

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

这里是选择器视图控制器类:

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

并且是我的表视图控制器中的一对方法:

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对象数组
NSURLSession.sharedSession().dataTaskWithURL()

它被正确填充,所以我不知道我在哪里犯了错误。 你能帮帮忙吗?

更新 非常感谢@NickCatib,这是我的工作代码。 注意:不需要另外一个类作为Picker视图委托。我刚刚在自定义单元格中实现了它:

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

1 个答案:

答案 0 :(得分:1)

好吧,我会在tableView控制器或单元格本身中为选择器视图设置委托。基本上我认为处理UIPicker这些变化的最佳方法是立即进行,因此请尝试在单元格内delegate实施UITableViewController。由于有机会有多个选择器,我会在这个特殊情况下在单元内处理它们。