2017-01-16 98 views
0

我已創建一個項目。在此使用tableview方法。我已經運行這個項目,所以運行成功。和工作良好但我有一些問題創建。我有很多次嘗試但沒有解決。所以請建議我如何解決這個問題。如何解決此錯誤:'indexPath'的本地聲明隱藏實例變量

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

LocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htrcell"]; 
if (cell==nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
[cell.location_lbl setTitle:location[indexPath.row]forState:UIControlStateNormal] ; 

cell.backgroundColor=[UIColor clearColor]; 
cell.bg_view.layer.cornerRadius = 20; 
cell.bg_view.layer.masksToBounds = YES; 
cell.bg_view.backgroundColor = [[UIColor colorWithRed:20.0/255 green:100.0/255 blue:171.0/255 alpha:0.7f] colorWithAlphaComponent:0.7f]; 

//////////////// 
[cell.map addTarget:self action:@selector(buttonClickedMap:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.map setTag:indexPath.row];  

[cell.phone addTarget:self action:@selector(buttonClickedPhone:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.phone setTag:indexPath.row]; 

[cell.location_lbl addTarget:self action:@selector(buttonClickedlocation:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.location_lbl setTag:indexPath .row]; 


return cell; 
} 

在這種cellForRowAtIndexPath方法的所有indexPath地方顯示的問題。像這條線在此

[cell.location_lbl setTitle:location[indexPath.row]forState:UIControlStateNormal]; 

indexPath的地方顯示的問題:

indexPath局部聲明隱藏實例變量。

如何解決我的問題。 Thankyou

+2

你有一個全局變量,在頭文件中有相同的名字「indexPath」。將其更改爲「myIndexPath」或其他內容。 – GeneCode

+0

我將「indexPath」替換爲「myindexPath」? –

+0

是的,你替換,而不是在方法「cellForRowAtIndexPath」中,在你的全局變量的定義中。 –

回答

0

我敢打賭,你已經聲明瞭變量,在你的類接口中名爲「indexPath」。改變這個名字。

+0

我不能理解,請簡要解釋 –

+0

檢查你在這個類中的位置(.m和.h文件)你在哪裏聲明瞭任何變量,並且可能其中一個名爲「indexPath」,它與變量名稱完全相同,參數在方法「cellForRowAtIndexPath」。將你創建的變量'indexPath'重命名爲任何其他名字 –

+1

它的一個愚蠢的錯誤,我改變變量名稱並解決我的問題。謝謝你這麼多:) –

0

如果任何時候,它顯示'yourVairableName'的本地聲明隱藏了實例變量,那麼它意味着您創建了與您的實例變量相同的本地變量名稱。

所以請檢查並確認您沒有創建任何與indexPath同名的本地變量。