2 iiabby iiabby 于 2014.10.08 14:46 提问

iOS开发,UIPickerView设置了datasource却不显示数据问题,大神求解

@protocol MyViewDataSource ;
@protocal MyViewDelegate ;
@interface MyView : UIView //自定义view
{
id dataSource
id delegate
UIToolbar toolbar
UIPickerView pickerView
}
@interface ViewA : UITableView
{
}
@interface ViewControllerA : UITableViewController//在这个controller里启动MyView
{
MyView myView
}

1.在ViewControllerA 中实现MyViewDataSource和MyViewDelegate也就是UIPickerView的datasource和delegate
2.在ViewControllerA中设置myView.dataSource = self和myView.delegate=self
3.在MyView里,设置UIPickerView的dataSource和delegate,pickerView.dataSource = self.dataSource ,pickerView.delegate=self.delegate
为什么UIPickerView不显示数据!!!???

1个回答

lxj0596
lxj0596   2015.07.15 22:11

给你参考一下:


import UIKit

class ViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {

    var pickerData : NSDictionary!
    var pickerProvincesData : NSArray!
    var pickerCitiesData : NSArray!

    @IBOutlet weak var pickerView1: UIPickerView!
    @IBOutlet weak var label1: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //读取省市资源信息
        let plistPath = NSBundle.mainBundle().pathForResource("provinces_cities", ofType: "plist")
        let dict = NSDictionary(contentsOfFile: plistPath!)
        //全部数据
        self.pickerData = dict
        //省份数据
        self.pickerProvincesData = self.pickerData.allKeys

        //默认读取第一个省的所有市数据
        let selectedProvince = self.pickerProvincesData[0] as! NSString
        self.pickerCitiesData = self.pickerData[selectedProvince] as! NSArray
        //
        self.pickerView1.dataSource = (self as! UIPickerViewDataSource)
        self.pickerView1.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //选择器中拨轮的数目,省\市,就是2,若仅选择省就是1
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 2
    }

    //实现DataSource方法,必须,否则编译无法通过。返回显示的数据源数量
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if(component == 0)
        {
            return self.pickerProvincesData.count
        }
        else
        {
            return self.pickerCitiesData.count
        }
    }

    //实现Delegate方法,根据row为索引,获取对应行的数据
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        if(component == 0)
        {
            return self.pickerProvincesData[row] as! String
        }
        else
        {
            return self.pickerCitiesData[row] as! String
        }
    }

    //选择省份后响应
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if(component == 0)
        {
            let selectedProvince = self.pickerProvincesData[row] as! String
            self.pickerCitiesData = self.pickerData[selectedProvince] as! NSArray
            self.pickerView1.reloadComponent(1)
            //切换省后,市默认选择第一项
            //selectRow(row参数表示inComponent所索引代表列的第row行。列\行,顺序颠倒
            pickerView1.selectRow(0, inComponent: 1, animated: true)
        }
    }

    //获取当前选择结果
    @IBAction func onClick(sender: AnyObject) {
        let row1 = self.pickerView1.selectedRowInComponent(0)
        let row2 = self.pickerView1.selectedRowInComponent(1)
        let selected1 = self.pickerProvincesData[row1] as! String
        let selected2 = self.pickerCitiesData[row2] as! String
        let title = NSString(format : "%@%@", selected1, selected2)
        label1.text = title as String
    }
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!