我有一个表格,里面有一个单元格。在Cell中有一个Picker View
现在我想将数据(一个对象数组)从父控制器传递到表视图单元格,这样我就可以在Picker View中显示数据
var foodServings:[FoodUnit]=[]
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("recipeIngredientCell",forIndexPath: indexPath) as! ReceiptIngredientsViewCell
let row = indexPath.row
cell.ingredientName.text = foods[row].name
cell.ingredientText.text = foodQuantities[row]
// cell.foodServings = self.foodServings
return cell
}
我希望我可以将数据发送到Cell,但这不起作用。
我试着稍微澄清一下:
Thats how the table is constructed
使用:
cell.ingredientName.text = foods[row].name
cell.ingredientText.text = foodQuantities[row]
我可以访问单元格标签,例如bean和textview中的文本
但要填充选择器视图,我需要向每个单元格发送一组数据,以便我可以填充选择器视图。
答案 0 :(得分:1)
如果您只是传递数据,可以使用全局变量/构造函数将值传递给单元格。
答案 1 :(得分:0)
请检查这些:
foods
数组是否填充了您的数据?foodQuantities
数组是否填充了您的数据?foods.count
答案 2 :(得分:0)
首先,您可以查看有关UIPickerView
您是否已将UIPickerViewDelegate
和UIPickerViewDataSource
连接到viewController?
之后,请确保您已实现这些委托方法:
// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return foodServings.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return foodServings[row]
}
// Catpure the picker view selection
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// This method is triggered whenever the user makes a change to the picker selection.
// You can update your cell's UI here. For example:
let indexpath = NSIndexPath(row: , section:)
let cell = self.tableView(tableView, cellForRowAt: indexPath)
cell.yourTextField.text = foodServings[row]
}
根据您提供给我们的信息,我只能说明您的foodServings
数组是空的,所以您能告诉我这堂课的所有代码吗?