Reputation: 515
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 ?
Upvotes: 1
Views: 145
Reputation: 3753
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)
}
}
Upvotes: 1
Reputation: 14040
try and change your prepareforsegue to the following:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mapSegue" {
if let destination = segue.destinationViewController as? Map, button = sender as? UIButton, rubIndex = tableView.indexPathForRowAtPoint(button.convertPoint(CGPointZero, toView: tableView)) {
print(rubIndex.row)
destination.nomVariable = rubIndex.row
}
}
}
}
Upvotes: 1