如何在自定义 tableViewCell 中初始化 UiPickerView?
How to initialize UiPickerView inside custom tableViewCell?
我正在尝试在 tableViewCell
中创建 PickerView
。我让我的自定义单元格符合 UIPickerViewDelegate
和 UIPickerViewDataSource
协议。我将数据数组从控制器发送到单元格(我认为这不符合 MVC 模式,也许你也可以建议我如何解决这个问题?)。但是当 tableview 调用 dequeueReusableCellWithIdentifier
时,单元格调用 pickerView 函数。但是,pickerView 函数使用尚未初始化的 pickerData。我该如何解决?
下面是我的单元格代码:
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]
}
}
这里是单元初始化的代码:
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
中做一个小的改变,然后在设置 [=15= 之后重新加载 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
}
也在awakeFromNib
初始化你的pickerData对象
override func awakeFromNib() {
self.pickerData = Array<String>()
self.picker.delegate = self;
self.picker.dataSource = self;
super.awakeFromNib()
}
我正在尝试在 tableViewCell
中创建 PickerView
。我让我的自定义单元格符合 UIPickerViewDelegate
和 UIPickerViewDataSource
协议。我将数据数组从控制器发送到单元格(我认为这不符合 MVC 模式,也许你也可以建议我如何解决这个问题?)。但是当 tableview 调用 dequeueReusableCellWithIdentifier
时,单元格调用 pickerView 函数。但是,pickerView 函数使用尚未初始化的 pickerData。我该如何解决?
下面是我的单元格代码:
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]
}
}
这里是单元初始化的代码:
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
中做一个小的改变,然后在设置 [=15= 之后重新加载 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
}
也在awakeFromNib
初始化你的pickerData对象
override func awakeFromNib() {
self.pickerData = Array<String>()
self.picker.delegate = self;
self.picker.dataSource = self;
super.awakeFromNib()
}