2012-06-19 48 views
0

我想知道爲什麼這不起作用請幫助我。 這裏是我的代碼如果不能使用索引路徑

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath 

*)indexPath 
{ 
    if (selectedIndexPath == indexPath) { 
     selectedIndexPath = nil; 
    } else { 
     selectedIndexPath = indexPath; 
    } 
    [self.tableView deselectRowAtIndexPath : indexPath animated : NO]; 
    [tableView beginUpdates]; 
    [tableView endUpdates]; 
} 
+0

什麼是selectedIndexPath類型/聲明? –

+0

它是以前點擊過的索引路徑 – ohmprakash

+0

它的類型是nsindexpath。 – ohmprakash

回答

0

請務必保留您selectedIndexPath所以它沒有被設置爲nil在不經意間。

@interface YourClass 

@property (nonatomic, strong) NSIndexPath *selectedIndexPath; 

@end 

然後參考變量self.selectedIndexPath,你應該沒問題。

但還有第二個警告。 NSIndexPath是一個對象,因此運算符==只會比較指針地址。顯然,那些將永遠是不同的,因爲一個是你的變量,另一個是方法的私有變量!你想用什麼或者是

if ([indexPath isEqualTo:selectedIndexPath]) // not tested 

if (indexPath.row == selectedIndexPath.row && 
    indexPath.section == selectedIndexPath.section) 
+0

謝謝。它工作正常。 – ohmprakash

0

NSIndexPath對象由兩個部分組成,他們是行和部分。 indexPathForRow:inSection:方法根據行和節索引編號創建一個NSIndexPath對象。確保您的selectedIndexPath已正確創建。請按照此apple reference瞭解更多詳情。