2016-10-17 177 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

如果您只傳遞數據,則可以使用全局變量/構造函數將值傳遞給單元格。

相關問題