简体   繁体   English

将数据快速传递给tableView单元格

[英]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:请检查这些:

  • is the cell with "recipeIngredientCell" identifier is there in your storyboard?带有“recipeIngredientCell”标识符的单元格是否在您的故事板中?
  • is foods array is filled with your data? foods数组是否充满了您的数据?
  • is foodQuantities array is filled with your data? foodQuantities数组是否填充了您的数据?
  • have you implemented tableView(_:numberOfRowsInSection:) method?你实现了 tableView(_:numberOfRowsInSection:) 方法吗? This method must return number of your rows, for example 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?您是否将UIPickerViewDelegateUIPickerViewDataSource连接到您的 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.

相关问题 Swift TableView传递所选单元格 - Swift TableView pass selected Cell 传回Tableview单元格数据以在Swift中的PFQueryTableViewController中过滤对象 - Pass back Tableview Cell Data to filter objects in my PFQueryTableViewController in Swift 数据将tableview单元格传递到Swift中的标签栏视图控制器 - Data pass tableview cell to tab bar view controller in Swift 如何将数据从 TableView 单元格传递到 Swift 5 中的另一个 viercontroller? - How to pass data from TableView cell to another viercontroller in Swift 5? Swift / tableView使用InstantiateViewControllerWithIdentifier传递数据 - Swift / tableView pass data with instantiateViewControllerWithIdentifier 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview? 快速将TableView单元中的数据传递给视图控制器 - Swift Passing Data in a tableview cell to a view controller Swift bool数据未传递到TableView单元格 - Swift bool data not passed to tableview cell 传递数据取决于tableView单元格中的按钮 - Pass data depends on the button in tableView cell 通过点击TableView单元格将数据传递到详细信息视图 - Pass Data to Detail View by Tapping TableView Cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM