簡體   English   中英

將數據快速傳遞給tableView單元格

[英]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 中的文本

但是要填充選擇器視圖,我需要向每個單元格發送一組數據,以便我可以填充選擇器視圖。

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

請檢查這些:

  • 帶有“recipeIngredientCell”標識符的單元格是否在您的故事板中?
  • foods數組是否充滿了您的數據?
  • foodQuantities數組是否填充了您的數據?
  • 你實現了 tableView(_:numberOfRowsInSection:) 方法嗎? 此方法必須返回行數,例如foods.count

首先,你可以查看這個關於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數組是空的,所以你能告訴我這個類的所有代碼嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM