uitableview - 如何快速获取所选行的textLabel?

标签 uitableview swift ios8 tableview xcode6

所以我试图获取我选择的行的 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 版本”是什么?

最佳答案

试试这个:

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)

关于uitableview - 如何快速获取所选行的textLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26158768/

相关文章:

即使没有碰撞位掩码,swift skspritenode 也会发生碰撞

ios - 使用 UIModalPresentationCustom 在 ModalViewController 中设置框架 iOS8

ios - "Use Auto Layout"和 "Use Size Classes"有什么区别

IOS - 自调整单元格问题

swift - 在照片项目扩展中显示选定的照片

swift - 如何快速获取 Collection View 的标题?

xcode - 发布应用程序时捆绑问题

ios - 将对象附加到控件以指示 UITableView 中的状态

ios - UITableView 内的 CollectionView Cell 不会水平滚动以获得 Voice Over 可访问性

ios - removeObjectForKey 不会从 .plist 中删除值