通过单元格中的按钮通过segue传递数据......不工作......

时间:2016-04-03 16:01:09

标签: ios swift uitableview segue

我在自定义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

无论选择何种细胞。我做错了什么?

2 个答案:

答案 0 :(得分:1)

好的,在这种情况下,当您在当前索引的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)
    }
}

答案 1 :(得分:0)

尝试将准备工作改为以下内容:

Function AssignColor(r As Integer, g As Integer, b As Integer)
    ActiveCell.Interior.color = RGB(r, g, b)
    AssignColor = "#" & Application.WorksheetFunction.Dec2Hex(RGB(r, g, b))
End Function