2013-04-30 25 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