[英]Pass data to tableView cell swift
我有一個表格,里面有一個單元格。 在單元格內有一個選擇器視圖
現在我想將數據(一組對象)從父控制器傳遞到表視圖單元格,以便我可以在選擇器視圖中顯示數據
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.ingredientName.text = foods[row].name
cell.ingredientText.text = foodQuantities[row]
我可以訪問單元格標簽,例如 bean 和 textview 中的文本
但是要填充選擇器視圖,我需要向每個單元格發送一組數據,以便我可以填充選擇器視圖。
如果您只是傳遞數據,則可以使用全局變量/構造函數將值傳遞給單元格。
請檢查這些:
foods
數組是否充滿了您的數據?foodQuantities
數組是否填充了您的數據?foods.count
首先,你可以查看這個關於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
數組是空的,所以你能告訴我這個類的所有代碼嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.