如何在自定义tableViewCell中初始化UiPickerView? [英] How to initialize UiPickerView inside custom tableViewCell?

查看:69
本文介绍了如何在自定义tableViewCell中初始化UiPickerView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 tableViewCell 中创建 PickerView .我使我的自定义单元格符合 UIPickerViewDelegate UIPickerViewDataSource 协议.我从控制器向单元发送数据数组(我认为这不符合MVC模式,也许您也可以建议我如何解决该问题?).但是,当tableview调用 dequeueReusableCellWithIdentifier 时,单元格将调用pickerView函数.但是,pickerView函数使用尚未初始化的 pickerData .我该如何解决?

I am trying to create PickerView inside tableViewCell. I made my custom cell conform UIPickerViewDelegate and UIPickerViewDataSource protocols. I send data array to cell from the controller (I think this doesn't conform to MVC pattern, may be you also can suggest me how to fix that?). But when tableview calls dequeueReusableCellWithIdentifier the cell calls pickerView function. However, pickerView function uses pickerData which is not initialized yet. How do I fix that?

下面是我的单元格代码:

Below is my code for the cell:

class PickerTableViewCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var picker: UIPickerView!
    @IBOutlet weak var title: UILabel!
    var pickerData: Array<String>!
    override func awakeFromNib() {
        self.picker.delegate = self;
        self.picker.dataSource = self;
        super.awakeFromNib()

    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count // I get fatal error here due to pickerData is nil
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[row]
    }


}

这是单元格初始化的代码:

And here is the code for the cell's initialization:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("picker", forIndexPath: indexPath) as! PickerTableViewCell
    cell.title.text = fieldModel.editFieldArray[indexPath.row].title
    cell.pickerData = (fieldModel.editFieldArray[indexPath.row] as! PickerEditField).pickerData
    return cell
}

非常感谢您的帮助!

推荐答案

您的问题是您已经重新加载了 PickerView 的组件,因此请在您的 cellForRowAtIndexPath 中做一个小的更改并在设置 pickerData 数组后重新加载 PickerView 的组件.

Your issue is that you have reload the components of PickerView so make one small change in your cellForRowAtIndexPath and just reload the components of PickerView after setting the pickerData Array.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("picker", forIndexPath: indexPath) as! PickerTableViewCell
    cell.title.text = fieldModel.editFieldArray[indexPath.row].title
    cell.pickerData = (fieldModel.editFieldArray[indexPath.row] as! PickerEditField).pickerData
    cell.picker.reloadAllComponents();
    return cell
}

也在 awakeFromNib 中初始化您的pickerData对象

Also in awakeFromNib initialize your pickerData object

override func awakeFromNib() {
    self.pickerData = Array<String>()
    self.picker.delegate = self;
    self.picker.dataSource = self;
    super.awakeFromNib()

}

这篇关于如何在自定义tableViewCell中初始化UiPickerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆