认为没有选择行号 [英] Segue not getting selected row number

查看:126
本文介绍了认为没有选择行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am passing data from a table view controller to a detail view. I tried using indexPath.row directly in my prepareForSegue method, however it displays an error of


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

use of unresolved identifier 'indexPath'

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

So, after searching the web, I set up the variable indexOfSelectedPerson which is assigned the value of indexPath.row. The problem when I run the app in the simulator is that prepareForSegue is getting the initial value of indexOfSelectedPerson (0), then getting the value of the selected row only after I click it. So, when I hit the back button in the Simulator and select a different row, the detail view shows the info of the row I selected the previous time.

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])"
            }
        }
    }
}

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

So, selecting Doug when the app first starts in the simulator displays the details for Bob because indexPathOfSelectedPerson is 0. Hitting the back button and then selecting Jill displays the details for Doug because indexPathOfSelectedPerson became 1 when I clicked on Doug the previous time. I'm guessing the problem stems from the order in which the methods are called.

推荐答案

做这种方法的最佳方法事情是不使用委托。

The best way to do this kind of thing is not to use the delegate.

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

这篇关于认为没有选择行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆