所以我试图获取我选择的行的 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/