[英]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. 我尝试使用
didSelectRowAtIndexpath
和perparesegue
函数,但没有解决。
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.
假设您正在使用情节提要。
CTRL drag from the prototype TableViewCell in your storyboard to the ViewController you want to pass data to. CTRL从情节提要中的原型TableViewCell拖动到要将数据传递到的ViewController。
Choose Show for the segue type and give it an identifier 选择显示作为搜索类型并为其指定一个标识符
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.