PickerView没有显示任何价值。它会给表格单元重复使用没有索引路径的警告?

时间:2016-07-26 06:07:14

标签: ios objective-c iphone swift uipickerview

我创建了一个UIView类,我在其中声明了pickerView并实现了所有必要的委托。当我点击文本字段时,pickerView中没有显示任何值,日志中出现警告

  

没有重复使用表格单元格的索引路径

请帮我找出解决方案。

import UIKit

class CustomPickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {

var pickerView = UIPickerView()
var pickerString: String?
var pickerArray = NSArray()

class var instance: CustomPickerView
{
    let pickerVC = CustomPickerView()
    return pickerVC
}

override init(frame: CGRect) {
    super.init(frame : frame)

    pickerView.delegate = self
    pickerView.dataSource = self
    pickerView.backgroundColor = UIColor.yellowColor()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setValueOfPicker(text: String, array: NSArray, textField: UITextField) -> Void {
    pickerString = text
    pickerArray = array
    setPicker(textField)
    pickerView.reloadAllComponents()
    pickerView.selectRow(0, inComponent: 0, animated: true)
}

//MARK:
//MARK: Piker View Delegate & DataSource
//MARK:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerArray.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerString == "country" {
        return pickerArray[row].valueForKey("country") as? String
    }
    else if pickerString == "printCoupon"
    {
        return pickerArray[row] as? String
    }
    return nil
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

}

func setPicker(dataTextField: UITextField)
{
    dataTextField.inputView = pickerView;
    let doneToolbar: UIToolbar = UIToolbar()

    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: #selector(donePickerAction))
    let cancle: UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Done, target: self, action: #selector(canclePickerAction))

    var items = [UIBarButtonItem]()
    items.append(cancle)
    items.append(flexSpace)
    items.append(done)
    doneToolbar.sizeToFit()
    doneToolbar.items = items

    dataTextField.inputAccessoryView = doneToolbar
}

1 个答案:

答案 0 :(得分:0)

尝试实施此方法

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView
{
    let pickerLabel = UILabel()

    if pickerString == "country" {
        pickerLabel.text = pickerArray[row].valueForKey("country") as? String
    }
    else if pickerString == "printCoupon"
    {
        pickerLabel.text = pickerArray[row] as? String
    }

    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
}