當使用UICollectionView時,我很困惑獲取didSelectItemAtIndexPath
方法的indexPath
值。indexPath UICollectionView的值
我使用下面的代碼行中didSelectItemAtIndexPath
方法:
//FileList is an NSArray of files from the Documents Folder within my app.
//This line gets the indexPath of the selected item and finds the same index in the Array
NSString *selectedItem = [NSString stringWithFormat:@"%@",[FileList objectAtIndex:indexPath.item]];
//Now the selected file name is displayed using NSLog
NSLog(@"Selected File: %@", selectedItem);
的問題是,所述indexPath總是返回0(見下面的代碼),結果僅在第一項FileList NSArray被選中。
我已經嘗試了不同的參數,如
indexPath.row
indexPath.item
- ,只是普通的
indexPath
所有的這些在下面的NSLog語句返回值0:
NSLog(@"index path: %d", indexPath.item); //I also tried indexPath.row here
也許我只是格式化NSString不正確,但我不認爲這是事實,因爲沒有任何警告,我試圖在其他地方以不同的格式。
爲什麼indexPath始終返回0?
任何幫助,將不勝感激!謝謝!
請說明如何格式化'NSLog'的字符串 – dasblinkenlight
'.item'在這裏是合適的屬性,但功能上與'.row'沒有什麼不同。你需要展示更多 - 你怎麼知道它是零?什麼是FileList? – jrturton
我編輯了這個問題,包括了我如何格式化NSLog字符串,並且我已經闡明瞭FileList是什麼(請閱讀註釋代碼) –