Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how know if uitableview has a selected cell

I'm developing an App and at some point I have several uitableview. I want to know outside the delegate methods, an action for instance, if the tableviews have a selected cell and which one.

I tried to use: (NSIndexPath *)indexPathForSelectedRow but it doesn't work, because if I don't have a selected cell the [(NSIndexPath *) row] returns "0" and not nil

could you please give any help??

thanks..

like image 868
Frade Avatar asked Jul 21 '11 11:07

Frade


1 Answers

Your method is correct - indexPathForSelectedRow indeed returns nil if no cell is selected. But if you try to send message to nil object and use the value returned it will be 0, so you need to test if path value is nil or not before trying to get cell's row from it:

NSIndexPath *path = [table indexPathForSelectedRow];
if (path){
   row = [path row];
   ...
}
else{
 // No cell selected
}

P.S. nil is just a typecasted 0, so in practise they are the same thing.

like image 57
Vladimir Avatar answered Sep 27 '22 22:09

Vladimir