我在自定义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 0No 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.rowThen 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) } }更多推荐
发布评论