获取TableView中cell中的label的text出错!

发布于 2022-09-02 12:33:16 字数 607 浏览 11 评论 0

    //通过唯一表示符搜索
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //获取视图
        let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("content") as! ViewController
        //获取当前点击的cell
        let cellIndex = tableView .cellForRowAtIndexPath(indexPath)
        //给标题赋值
        viewController.rowIndex = "\(cellIndex?.detailTextLabel?.text)"       
        //跳转压栈
        self.navigationController?.pushViewController(viewController, animated: true)
    }

获取到的结果是空的,nil。请问是我获取的方式不对还是思路不对呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青柠芒果 2022-09-09 12:33:16

一般思路应该是这样的:你的 UITableView 需要展示的数据是放在一个 NSArray 类似的容器中,点击某个 Cell 时获取这个 Cell 对应的数据,是在 UITableView 点击 Cell 的回调方法中获取这个 Cell 的 IndexPath,再用这个 IndexPath 去 NSArray 里取得数据,而不是直接从 Cell 上拿数据。

天荒地未老 2022-09-09 12:33:16

你用的是UItableviewCell吗?

你与清晨阳光 2022-09-09 12:33:16
//通过唯一表示符搜索
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //获取视图
        let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("content") as! ViewController
        
        //方法一
        //获取当前点击的cell
        let cellIndex = tableView .cellForRowAtIndexPath(indexPath)
        //获取cell中的label对象
        var textLabel : UILabel? {
            get{
                return cellIndex?.textLabel;
            }
        }
        //提取label中的text属性内容,并将其传入视图属性
        viewController.rowLabelText = (textLabel?.text)!
        
        //方法二(精简方法一)
        viewController.rowLabelText = (cellIndex?.textLabel?.text)!
        
        //跳转压栈
        self.navigationController?.pushViewController(viewController, animated: true)
    }

可以通过上诉两个方法获取!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文