怎么知道uitableview是否有一个选定的单元格 [英] how know if uitableview has a selected cell
问题描述
我正在开发一个应用程序,在某些时候我有几个uitableview。
我想在委托方法之外知道,例如一个动作,如果tableviews有一个选定的单元格和哪一个。
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.
我试图使用:(NSIndexPath *)indexPathForSelectedRow
但它不起作用,因为如果我没有选定的单元格 [(NSIndexPath *)行]
返回0而不是nil
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
你能给予任何帮助吗?
谢谢..
推荐答案
您的方法是正确的 - 如果没有选择单元格,indexPathForSelectedRow确实返回nil。但是如果你尝试向nil对象发送消息并使用返回的值它将为0,所以你需要在尝试从它获取单元格的行之前测试路径值是否为nil:
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
}
PS nil只是一个类型0,所以在实践中它们是相同的。
P.S. nil is just a typecasted 0, so in practise they are the same thing.
这篇关于怎么知道uitableview是否有一个选定的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!