2012-02-04 112 views
2

如果我有一個索引路徑,其值爲{0,0}。將其更改爲{0,1}的正確方法是什麼?我知道,如果它是一個普通的C數組這純粹是:更改NSIndexPath對象的值

unsigned i_array[] = { 0, 0}; 
i_array[1] = 1; 

但從NSIndexPath的文檔,我可以得到最接近的是:

– indexPathByRemovingLastIndex 
– indexPathByAddingIndex: 

這似乎有點麻煩。有沒有一種方法可以覆蓋索引數組的最後一個成員?

+1

嘗試像'indexPath = [NSIndexPath indexPathForRow:indexPath.row切入口:indexPath.section + 1];'。 – 2012-02-04 09:17:16

+0

感謝您的評論和善意的幫助。 – Stanley 2012-02-04 09:32:02

回答

10

NSIndexPath不是數組。這是一個具有2個只讀屬性的對象:rowsection。你無法改變他們,但是,您可以創建基於舊的一個 indexPath:

NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:oldIndexPath.row inSection:1]; 
oldIndexPath = newIndexPath; 
+0

感謝您的回答。您的答案使用便捷的方法來創建newIndexPath,因此不需要手動發佈。因此,如果使用init方法,必須添加一個發佈聲明,是嗎? – Stanley 2012-02-04 09:24:20

+0

是的,如果你不使用ARC。此方法返回自動釋放對象。 – akashivskyy 2012-02-04 09:27:22

+0

感謝您的協助.. – Stanley 2012-02-04 09:31:20

0
indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; 
+0

請解釋您的答案,並且不要僅提供代碼片段。 – Jan 2016-02-16 11:59:59