如何解决此错误:'indexPath'的本地声明隐藏实例变量
我已创建一个项目。在此使用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
我敢打赌,你已经声明了变量,在你的类接口中名为“indexPath”。改变这个名字。
我不能理解,请简要解释 –
检查你在这个类中的位置(.m和.h文件)你在哪里声明了任何变量,并且可能其中一个名为“indexPath”,它与变量名称完全相同,参数在方法“cellForRowAtIndexPath”。将你创建的变量'indexPath'重命名为任何其他名字 –
它的一个愚蠢的错误,我改变变量名称并解决我的问题。谢谢你这么多:) –
如果任何时候,它显示'yourVairableName'的本地声明隐藏了实例变量,那么它意味着您创建了与您的实例变量相同的本地变量名称。
所以请检查并确认您没有创建任何与indexPath同名的本地变量。
你有一个全局变量,在头文件中有相同的名字“indexPath”。将其更改为“myIndexPath”或其他内容。 – GeneCode
我将“indexPath”替换为“myindexPath”? –
是的,你替换,而不是在方法“cellForRowAtIndexPath”中,在你的全局变量的定义中。 –