认为没有选择行号

时间:2014-07-04 15:57:28

标签: swift segue ios8 xcode6

我将数据从表视图控制器传递到详细信息视图。我尝试在indexPath.row方法中直接使用prepareForSegue,但它显示错误

  

使用未解析的标识符'indexPath'

因此,在搜索网络后,我设置了变量indexOfSelectedPerson,该变量的值为indexPath.row。我在模拟器中运行应用程序时的问题是prepareForSegue获取初始值indexOfSelectedPerson(0),然后仅在我单击它之后获取所选行的值。因此,当我点击模拟器中的后退按钮并选择另一行时,详细视图会显示我上一次选择的行的信息。

import UIKit

class MasterTableViewController: UITableViewController {

    var people = []
    var indexOfSelectedPerson = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        people = ["Bob", "Doug", "Jill"]
    }

    override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
       return 1
    }

    override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
        return people.count
    }

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

        let cell = tableView!.dequeueReusableCellWithIdentifier("personCell", forIndexPath: indexPath) as UITableViewCell

        cell.text = "\(people[indexPath.row])"

        return cell
    }

    override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
    {
        indexOfSelectedPerson = indexPath.row
    }


    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {

        if let mySegue = segue.identifier {

            if mySegue == "personDetails" {

                let detailsVC: DetailTableViewController = segue.destinationViewController as DetailTableViewController

                detailsVC.selectedPersonName = "\(people[indexOfSelectedPerson])"
            }
        }
    }
}

因此,当应用程序首次在模拟器中启动时选择Doug会显示Bob的详细信息,因为indexPathOfSelectedPerson为0.按下后退按钮然后选择Jill会显示Doug的详细信息,因为indexPathOfSelectedPerson变为1当我上一次点击Doug时。我猜测问题源于调用方法的顺序。

3 个答案:

答案 0 :(得分:18)

执行此类操作的最佳方法是不使用委托。

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    let selectedIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
    // Do your stuff with selectedIndex.row as the index
}

答案 1 :(得分:1)

Swift 3更新:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let selectedIndex = tableView.indexPath(for: sender as! UITableViewCell)!
    // Do your actual preparing here...
}

答案 2 :(得分:-1)

//In didSelectRowAtIndexPath, you use this code:

func tableView(tvMain: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        self.performSegueWithIdentifier("yourSegueName", sender: nil)

    }

//And prepareForSegue to get IndexPath and send your value

override func prepareForSegue(segue: UIStoryboardSegue,
        sender: AnyObject!){

            let indexPath : NSIndexPath = self.yourTableView.indexPathForSelectedRow()!
            //make sure that the segue is going to secondViewController
            let detailsVC = segue.destinationViewController as DetailTableViewController
            detailsVC.selectedPersonName = "\(people[indexPath.row])"

    }