swift – 认为没有选择行号

swift – 认为没有选择行号,第1张

概述我将数据从表视图控制器传递到详细信息视图.我尝试在prepareForSegue方法中直接使用indexPath.row,但它显示错误 use of unresolved identifier ‘indexPath’ 因此,在搜索Web之后,我设置了变量indexOfSelectedPerson,该变量被赋予indexPath.row的值.我在模拟器中运行应用程序时的问题是prepareForSe 我将数据从表视图控制器传递到详细信息视图.我尝试在prepareForSegue方法中直接使用indexPath.row,但它显示错误

use of unresolved IDentifIEr ‘indexPath’

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

import UIKitclass 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的详细信息,因为当我上一次单击Doug时indexPathOfSelectedPerson变为1.我猜测问题源于调用方法的顺序.

做这种事情的最好方法是不使用委托.
overrIDe func prepareForSegue(segue: UIStoryboardSegue!,sender: AnyObject!) {    let selectedindex = self.tableVIEw.indexPathForCell(sender as UItableVIEwCell)    // Do your stuff with selectedindex.row as the index}
总结

以上是内存溢出为你收集整理的swift – 认为没有选择行号全部内容,希望文章能够帮你解决swift – 认为没有选择行号所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1033172.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存