简体   繁体   English

ios Swift:将数据从tableView传递到视图控制器

[英]ios Swift: Passing data from tableView to view controller

enter image description here well this is my first time posting a question so not to sure how it works. 在这里输入图片说明很好,这是我第一次发布问题,因此不确定其工作原理。 I am doing an assignment on swift. 我正在迅速做作业。 I am stuck at a point where I need to pass the data from tableview to viewcontroller. 我陷入了需要将数据从tableview传递到viewcontroller的问题。 On my first ViewController, I have a list of data (Categories) coming from the database table and when the user clicks on any of the cell, it should go to the next viewcontroller the label becomes the heading. 在我的第一个ViewController上,我有一个来自数据库表的数据(类别)列表,当用户单击任何一个单元格时,它应该转到下一个ViewController,标签变为标题。 Please find my screen shot attached. 请找到我的屏幕截图。

Thanks 谢谢

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return values.count;
}    

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CategoryList_TableViewCell
    let maindata = values[indexPath.row]
    cell.categoryLabel.text = maindata["NAME"] as? String
    return cell;

}

I tried using didSelectRowAtIndexpath and perparesegue functions but not getting my head around. 我尝试使用didSelectRowAtIndexpathperparesegue函数,但没有解决。

can anyone can guide me. 谁能指导我。

Thanks a million in advance :) 预先感谢一百万:)

表格截图

You don't need to implement didSelectRow just to pass data through a segue. 您无需实现didSelectRow即可仅通过segue传递数据。 Assuming you're using Storyboards. 假设您正在使用情节提要。

  1. CTRL drag from the prototype TableViewCell in your storyboard to the ViewController you want to pass data to. CTRL从情节提要中的原型TableViewCell拖动到要将数据传递到的ViewController。

  2. Choose Show for the segue type and give it an identifier 选择显示作为搜索类型并为其指定一个标识符

  3. Do this in prepareForSegue: 在prepareForSegue中执行以下操作:

     if segue.identifier == "catView" { if let indexPath = self.tableView.indexPathForSelectedRow { let controller = segue.destinationViewController as! YourViewController let value = values[indexPath.row] controller.catTitleRec = value["NAME"] as! String } 

Do this in your didSelectRowAtIndexPath : didSelectRowAtIndexPath执行以下操作:

let maindata = values[indexPath.row]
self.performSegueWithIdentifier("yourIdentifier", sender: mainData)

Then, in your prepareForSegue function do this: 然后,在您的prepareForSegue函数中执行以下操作:

let destinationVC = segue.destinationViewcontroller as! YourCustomViewController
destinationVC.yourObject = sender as! YourObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从 tableview 单元格传递到 View Controller Swift 2.1 - Passing data from tableview cell to View Controller Swift 2.1 快速将TableView单元中的数据传递给视图控制器 - Swift Passing Data in a tableview cell to a view controller 在iOS Swift 2中将数据从视图控制器传递到表视图控制器 - Passing data from view controller to table view controller in ios swift 2 将字符串从TableView单元格传递到Swift 3中的View Controller - Passing a string from a TableView cell into a View Controller in Swift 3 将数据从父视图控制器传递到子视图控制器Swift 4 - Passing Data From Parent View Controller to Child View Controller Swift 4 将标签数据从tableview传递到另一个视图控制器 - Passing label data from a tableview to another view controller 目标C-将JSON数据从Tableview传递到另一个View Controller - Objective C - Passing JSON data from Tableview to another View Controller 在Swift中从视图控制器之间传递数据(从TableView到DetailViewController) - Passing data between View Controllers in Swift (From TableView to DetailViewController) 在Swift中的选项卡栏中将数据从TableView传递到View Controller - Pass data from tableview to view controller in tab bar in Swift 将数据从tableview传递到Swift中的选项卡栏视图控制器。 - Pass data from tableview to tab bar view controller in Swift.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM