2013-04-30 27 views
0

我想计算一个IndexPath的int值,它具有行和部分,然后用它们使用ObjectAtIndex从NSArray中选择一个对象。如何从IndexPath得到int

我只有从UITable字符串值,所以我试图将选择的indexpath值与我的实际数组的ObjectAtIndex路径。

这是我的代码到目前为止,但我得到一个错误,因为我试图使用indexPath代替int。

filterDataArray = [dataArrayOfDictionaries objectAtIndex:indexPath]; 

所以我的问题是我怎么能改变indexPath成int。任何帮助将不胜感激。

+0

的参考文档显示你的'UITableView'类别'NSIndexPath'。对这些文档的快速扫描将揭示“行”和“部分”属性。 – rmaddy 2013-04-30 03:24:37

+0

顺便说一句 - 你不想要一个'int'。这不是“objectAtIndex:'的'index'参数的类型。 – rmaddy 2013-04-30 03:25:32

+0

我想使用具有部分和行的索引路径来计算NSArray的objectAtIndex。 – HurkNburkS 2013-04-30 04:01:34

回答

0

请参阅this以更好地了解有关NSIndexPath的内容。

如果您仅使用NSArray和表视图(仅默认一个部分(如果未指定numberOfSections:方法))。尝试这个。

filterDataArray = [dataArrayOfDictionaries objectAtIndex:indexPath.row]; 
+0

这个剂量不起作用,因为我有一个表格视图与部分...所以它没有正确选择的索引路径与数组的对象。 – HurkNburkS 2013-04-30 03:55:41

+0

嗨,为了支持UITableView,你需要有一个数组和一个字典,这是实现它的最好方法。 – satheeshwaran 2013-04-30 05:24:17

2

所以你的情况,你可以同时使用这两种行和段值尝试这样的组合,

int totalRows = indexPath.row; //current section row value 


for (int n = 0; n < indexPath.section; n++) {//here current section value. 

    totalRows += [tableView numberOfRowsInSection:n];// here add rows in every section 
} 
+0

@HurkNburkS:它工作吗? – Balu 2013-04-30 04:49:16