如何在swift中获取所选行的textLabel? [英] How to get textLabel of selected row in swift?

查看:93
本文介绍了如何在swift中获取所选行的textLabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想获取我选择的行的textLabel的值。我试过打印它,但它不起作用。经过一些研究后,我发现这段代码有效,但仅限于Objective-C;

So i am trying to get the value of the textLabel of the row I select. I tried printing it, but it didn't work. After some research I found out that this code worked, but only in Objective-C;

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
    {
        NSLog(@"did select  and the text is %@",[tableView cellForRowAtIndexPath:indexPath].textLabel.text);]
    }

我找不到Swift的任何解决方案。虽然打印indexpath.row是可能的,但这不是我需要的。

I could not find any solution for Swift. Printing the indexpath.row is possible though, but that is not what I need.

所以我该怎么办?或者这段代码的'Swift-version'是什么?

so what should I do? or what is the 'Swift-version' of this code?

推荐答案

试试这个:

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

    let indexPath = tableView.indexPathForSelectedRow() //optional, to get from any UIButton for example

    let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell

    print(currentCell.textLabel!.text)

这篇关于如何在swift中获取所选行的textLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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