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?
我正在嘗試在tableViewCell中創建PickerView。我使自定義單元格符合UIPickerViewDelegate和UIPickerViewDataSource協議。我從控制器發送數據數組到單元格(我認為這不符合MVC模式,可能你也可以建議我如何解決這個問題?)。但是當tableview調用dequeueReusableCellWithIdentifier時,單元格會調用pickerView函數。但是,pickerView函數使用尚未初始化的pickerData。我該如何解決這個問題?
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
}
Thanks a lot for any help!
非常感謝您的幫助!
2
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.
您的問題是您已經重新加載PickerView的組件,因此在您的cellForRowAtIndexPath中進行一個小的更改,並在設置pickerData數組后重新加載PickerView的組件。
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
}
Also in awakeFromNib
initialize your pickerData object
同樣在awakeFromNib中初始化你的pickerData對象
override func awakeFromNib() {
self.pickerData = Array<String>()
self.picker.delegate = self;
self.picker.dataSource = self;
super.awakeFromNib()
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/09/14/7208dceea71615f1c4ccc9a770eefe1e.html。