Jonathan Neeskens Jonathan Neeskens - 1 year ago 116
Swift Question

How to get textLabel of selected row in swift?

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);]

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

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

Answer Source

Try this:

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

    let indexPath = tableView.indexPathForSelectedRow()

    let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download