加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

uitableview – 如何获取swift中所选行的textLabel?

发布时间:2020-12-14 05:56:40 所属栏目:百科 来源:网络整理
导读:所以我试图获取我选择的行的textLabel的值。我尝试打印它,但它没有工作。经过一番研究,我发现这段代码有效,但只有在Objective-C中; - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"did select
所以我试图获取我选择的行的textLabel的值。我尝试打印它,但它没有工作。经过一番研究,我发现这段代码有效,但只有在Objective-C中;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
    {
        NSLog(@"did select  and the text is %@",[tableView cellForRowAtIndexPath:indexPath].textLabel.text);]
    }

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

所以我该怎么做?或者这个代码的“Swift-version”是什么?

尝试这个:
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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读