通过单元格中的按钮通过segue传递数据...不工作......(Pass data through a segue via a button in a cell… Not working…)

编程入门 行业动态 更新时间:2024-10-22 04:51:50
通过单元格中的按钮通过segue传递数据...不工作......(Pass data through a segue via a button in a cell… Not working…)

我在自定义UITableView中有一个相当复杂的单元格。 该单元格中有多个元素,我希望能够在用户单击特定图像时显示地图控制器。

我首先尝试使用一个简单的按钮和那些功能:

import UIKit class SousCategorieViewController: UITableViewController { var rubSegue = NSDictionary() @IBOutlet weak var bigLogoOutlet: UIImageView! @IBOutlet weak var titreOutlet: UILabel! @IBOutlet weak var introOutlet: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let bigLogo = rubSegue["gfx"]!["bigLogo"]! bigLogoOutlet.image = UIImage(named: "\(bigLogo!)") let titre = rubSegue["gfx"]!["titre"]! titreOutlet.text? = "\(titre!)".uppercaseString self.title = titre as? String tableView.estimatedRowHeight = 3000 self.tableView.rowHeight = UITableViewAutomaticDimension } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table View override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return rubSegue["corpus"]!.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let corpusArray = rubSegue.objectForKey("corpus") as! NSArray let cell = tableView.dequeueReusableCellWithIdentifier("cellChapter", forIndexPath: indexPath) as! UniversalXIB cell.sousTitreOutlet.text = corpusArray[indexPath.row]["soustitre"] as? String cell.introOutlet.text = corpusArray[indexPath.row]["intro"] as? String cell.titreOutlet.text = corpusArray[indexPath.row]["titre"] as? String cell.imageOutlet.image = UIImage(named: (corpusArray[indexPath.row]["imageNP"] as! String)) let texteArray = corpusArray[indexPath.row].objectForKey("texteArray") as? NSDictionary let iconesArray = corpusArray[indexPath.row].objectForKey("iconesArray") as? NSDictionary deleteStackOrNot((corpusArray[indexPath.row]["iconesNPBool"] as! Bool), stack: cell.iconesStackOutlet) deleteImagesOrNot((corpusArray[indexPath.row]["mapBool"] as! Bool), nameImage: (corpusArray[indexPath.row]["map"] as! String), stackRoot: cell.titreOutletStack, outlet: cell.mapOutlet) return cell } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "mapSegue" { if let destination = segue.destinationViewController as? Map { if let rubIndex = tableView.indexPathForSelectedRow?.row { print(rubIndex) destination.nomVariable = rubIndex } } } } }

有3个单元格,唯一打印的是:

0 0

无论选择哪个细胞。 我究竟做错了什么 ?

I've got a rather complicated cell in a custom UITableView. There's multiple elements in that cell, and I would like to be able to show a map controller when the user is clicking on a specific image.

I first tried to do that with a simple button and those functions :

import UIKit class SousCategorieViewController: UITableViewController { var rubSegue = NSDictionary() @IBOutlet weak var bigLogoOutlet: UIImageView! @IBOutlet weak var titreOutlet: UILabel! @IBOutlet weak var introOutlet: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let bigLogo = rubSegue["gfx"]!["bigLogo"]! bigLogoOutlet.image = UIImage(named: "\(bigLogo!)") let titre = rubSegue["gfx"]!["titre"]! titreOutlet.text? = "\(titre!)".uppercaseString self.title = titre as? String tableView.estimatedRowHeight = 3000 self.tableView.rowHeight = UITableViewAutomaticDimension } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table View override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return rubSegue["corpus"]!.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let corpusArray = rubSegue.objectForKey("corpus") as! NSArray let cell = tableView.dequeueReusableCellWithIdentifier("cellChapter", forIndexPath: indexPath) as! UniversalXIB cell.sousTitreOutlet.text = corpusArray[indexPath.row]["soustitre"] as? String cell.introOutlet.text = corpusArray[indexPath.row]["intro"] as? String cell.titreOutlet.text = corpusArray[indexPath.row]["titre"] as? String cell.imageOutlet.image = UIImage(named: (corpusArray[indexPath.row]["imageNP"] as! String)) let texteArray = corpusArray[indexPath.row].objectForKey("texteArray") as? NSDictionary let iconesArray = corpusArray[indexPath.row].objectForKey("iconesArray") as? NSDictionary deleteStackOrNot((corpusArray[indexPath.row]["iconesNPBool"] as! Bool), stack: cell.iconesStackOutlet) deleteImagesOrNot((corpusArray[indexPath.row]["mapBool"] as! Bool), nameImage: (corpusArray[indexPath.row]["map"] as! String), stackRoot: cell.titreOutletStack, outlet: cell.mapOutlet) return cell } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "mapSegue" { if let destination = segue.destinationViewController as? Map { if let rubIndex = tableView.indexPathForSelectedRow?.row { print(rubIndex) destination.nomVariable = rubIndex } } } } }

There's 3 cells and the only thing that is printed is :

0 0

No matter what the selected cell. What am I doing wrong ?

最满意答案

好的,在这种情况下,当您在当前索引的cellForRowAtIndexPath中创建标签时,可以为该按钮添加标签:

button.tag = indexPath.row

然后在prepareForSegue中从发件人(按钮)中获取标签,然后该标签将成为您选择的单元格索引 - rubIndex:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let button = sender as? UIButton { let rubIndex = button.tag print(rubIndex) } }

Okay, in that case you could add a tag to the button when you create it in cellForRowAtIndexPath of the current index:

button.tag = indexPath.row

Then in prepareForSegue grab the tag from the sender (the button) which will then be your selected cell index - rubIndex:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let button = sender as? UIButton { let rubIndex = button.tag print(rubIndex) } }

更多推荐

本文发布于:2023-07-14 20:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107270.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   按钮   数据   工作   segue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!