[UILabel numberOfComponentsInPickerView:]: unrecognized selector sent to instance …]

浪子不回头ぞ 提交于 2020-01-16 05:18:06

问题


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

}

while this is called inside

 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

     ...
}

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?

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

回答1:


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.



来源:https://stackoverflow.com/questions/32522620/uilabel-numberofcomponentsinpickerview-unrecognized-selector-sent-to-instanc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!