2016-10-17 179 views
0

我有一个单元格的表格。内的小区有一个选择器视图传递数据到tableView单元格swift

enter image description here

现在我想从父控制器到表视图细胞传递数据(对象数组),所以我可以显示在选择器视图

数据
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 

} 

我希望我可以发送数据到单元,但这没有奏效。

我尝试clearify这一点:

Thats how the table is constructed

有:

cell.ingredientName.text = foods[row].name 
cell.ingredientText.text = foodQuantities[row] 

我可以访问例如豆细胞标签和文本中的TextView

但填充选择器视图我需要发送一个数据数组到每个单元格,以便我可以填充选择器视图。

+0

其实你可以和你使用的方式是正确的。你怎么了? –

+0

你做到了这一点,如果是的话,那么你如何将数据传递到tableview单元格 –

回答

0

请检查这些:

  • 与“recipeIngredientCell”标识的细胞是有你的故事板?
  • foods数组中填入您的数据?
  • foodQuantities数组中填入您的数据?
  • 你实现了tableView(_:numberOfRowsInSection :)方法吗?此方法必须返回你的行数,例如foods.count
+0

是的所有已经检查的问题是将一个对象数组传递给单元格,然后可以填充选择器视图 –

0

首先,你可以检查出this教程的UIPickerView

你连接UIPickerViewDelegateUIPickerViewDataSource你的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数组是空的,所以请你告诉我所有的代码在这个班上?

+0

也许我有点不准确。选择器视图的作品,但我需要传递一个数组与视图控制器的对象到单元格控制器,我可以稍后使用填充选择器视图 –

+0

@chrissychristancuvic然后,请精确到足以编辑您的问题,尝试把更多的代码,解释你现在拥有什么,你想要做什么,以及你的问题是什么,而不是放一块代码,让人们猜测你的意图。 –

1

如果您只传递数据,则可以使用全局变量/构造函数将值传递给单元格。

相关问题