[英]Pass data to tableView cell swift
I have a Table with a cell in it.我有一个表格,里面有一个单元格。 Within the Cell there is a Picker View
在单元格内有一个选择器视图
Now I want to pass data (an array of objects) from the Parent Controller to the Table View Cell so I can show the data in the 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
}
I hoped I can send Data to the Cell but this didnt work.我希望我可以将数据发送到单元格,但这不起作用。
I try to clearify it a bit:我试着澄清一下:
Thats how the table is constructed这就是表的构造方式
with:和:
cell.ingredientName.text = foods[row].name
cell.ingredientText.text = foodQuantities[row]
I can access the cell Labels for example bean and the text in the textview我可以访问单元格标签,例如 bean 和 textview 中的文本
But to populate the picker view i need to send an array of data to each cell so that i can populate the picker view.但是要填充选择器视图,我需要向每个单元格发送一组数据,以便我可以填充选择器视图。
如果您只是传递数据,则可以使用全局变量/构造函数将值传递给单元格。
Please check these:请检查这些:
foods
array is filled with your data? foods
数组是否充满了您的数据?foodQuantities
array is filled with your data? foodQuantities
数组是否填充了您的数据?foods.count
foods.count
First of all, you can check out this tutorial about UIPickerView
首先,你可以查看这个关于
UIPickerView
教程
Did you connect the UIPickerViewDelegate
and UIPickerViewDataSource
to your viewController?您是否将
UIPickerViewDelegate
和UIPickerViewDataSource
连接到您的 viewController?
After that, make sure you have implemented those delegate methods:之后,确保您已经实现了这些委托方法:
// 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]
}
From what you have provided us, I can only tell that your foodServings
array is empty, so could you please show me all the codes in this class?根据你提供的信息,我只能知道你的
foodServings
数组是空的,所以你能告诉我这个类的所有代码吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.