enter image description here好吧,这是我第一次发布问题,所以不确定它是如何工作的。我正在 swift 上做作业。我被困在需要将数据从 tableview 传递到 viewcontroller 的地方。在我的第一个 ViewController 上,我有一个来自数据库表的数据列表(类别),当用户单击任何单元格时,它应该转到下一个 viewcontroller,标签成为标题。请找到我的屏幕截图。
谢谢
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;
}
我尝试使用 didSelectRowAtIndexpath和 perparesegue功能,但我不明白。
谁能指导一下。
提前一百万致谢:)
最佳答案
您不需要为了通过 segue 传递数据而实现 didSelectRow。假设您正在使用 Storyboard。
CTRL 从 Storyboard中的原型(prototype) TableViewCell 拖动到您要将数据传递到的 ViewController。
为 segue 类型选择 Show 并为其指定一个标识符
在 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
}
关于ios swift : Passing data from tableView to view controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39998358/