错误:
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [__ NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0]'
这是我的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"AartiTableViewCell";
AartiTableViewCell *cell = (AartiTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AartiTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.btnFav.userInteractionEnabled=YES;
}
NSString *strfavarry = [NSString stringWithFormat:@"SELECT Title FROM %@ WHERE identifire='%@'",[FavTablename objectAtIndex:indexPath.row],[FavIdent objectAtIndex:indexPath.row]];
FavTitle = [FavData lookupAllForSQL:strfavarry];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.index = indexPath.row;
cell.btnFav.tag=indexPath.row;
[cell.btnFav setBackgroundImage:[UIImage imageNamed:@"unfav.png"] forState:UIControlStateNormal];
[cell.btnFav addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
cell.lbltitle.text=[FavTitle objectAtIndex:indexPath.row];
return cell;
}
答案 0 :(得分:0)
此错误意味着您正在尝试从索引1处的数组中获取元素,并且您将超出范围。
可能问题是:
[FavTablename objectAtIndex:indexPath.row]
在阅读之前检查你的数组是否有一些元素。
确保您的问题启用了异常断点:
答案 1 :(得分:0)
NSString *strfavarry = [NSString stringWithFormat:@"SELECT Title FROM %@ WHERE identifire='%@'",[FavTablename objectAtIndex:indexPath.row],[FavIdent objectAtIndex:indexPath.row]];
此行被赋予错误,因为您的numberofrows count大于这些数组计数值。
请检查您的numberofrows计数,FavTablename
和FavIdent
数组计数是否相同。
答案 2 :(得分:0)
正如您所说,FavTitle
和Favtablename
具有相同数量的元素。因此,请尝试在cellForRowAtIndexPath
中进行异常处理,如下所示: -
if(FavTitle.count > indexPath.row){
cell.lbltitle.text=[FavTitle objectAtIndex:indexPath.row];
}