didSelectRowAtIndex的indexPath错误

时间:2013-10-25 11:12:09

标签: ios json uitableview didselectrowatindexpath

我通过JSON解析产品ID,然后将其推送到详细视图并放入网址以解析有关所选产品的更多信息,但似乎didSelectRowAtIndexPath返回错误的indexPath,因为每次我点击如果没有为详细信息视图加载产品,则获取最后检索的行的产品ID。

例如,我显示了5行。我的逻辑正确地为每一行(第1行,第2行,第3行,第4行,第5行)解析所有ID。我知道命中第一行是因为我想获取链接到第1行的信息,但是不是第一行的信息,详细信息视图获取解析第5行的信息的命令,因为这是解析的最后一个ID,然后显示错误的信息。

我不知道如何让didSelectRowAtIndexPath方法获取正确行的正确信息。

以下是didSelectRowAtIndexPath的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *iPhone = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    CJArtikelDetailViewController *artikelView = [iPhone instantiateViewControllerWithIdentifier:@"detail"];

    NSString *detail = [NSString stringWithFormat:@"%@", wareID];

    artikelView.productID = detail;

    [self.navigationController pushViewController:artikelView animated:YES];
    [neuheitenTableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"IndexPath: %@", [indexPath description]);
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *neuheitenCell = @"neuheitenCell";
    CJHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:neuheitenCell];

    /*
    NSNumber *preis = [[arrayArtikelPreis objectAtIndex:indexPath.row] objectForKey:@"preis"];
    NSMutableString  *test = [NSMutableString stringWithFormat:@"€ %@", preis];
    cell.artikelPreis.text = test;
      */

    NSString *imageString = [[arrayNeuheiten objectAtIndex:indexPath.row] objectForKey:@"bild"];
    //NSLog(@"BildURL: %@", imageString);
    NSURL *imageURL = [NSURL URLWithString:imageString];
    [cell.artikelImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@""]];

    NSDictionary *object = [arrayNeuheiten objectAtIndex:indexPath.row];
    //NSLog(@"%@", object);

    wareID = [object objectForKey:@"ware_id"];
    NSLog(@"Waren ID: %@", wareID);

    cell.artikelName.text = [object objectForKey:@"name"];
    cell.artikelHersteller.text = [object objectForKey:@"lieferant_name"];


    return cell;

}

我必须让方法以某种方式确定选择了哪一行,但我不知道如何在文档中找到有关它的信息。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

希望这有帮助

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *iPhone = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    CJArtikelDetailViewController *artikelView = [iPhone instantiateViewControllerWithIdentifier:@"detail"];
    NSDictionary *object = [arrayNeuheiten objectAtIndex:indexPath.row];
    //NSLog(@"%@", object);
    //assign selected product id to wareId here and pass it to detail view controller it will work
    wareID = [object objectForKey:@"ware_id"];
    NSLog(@"Waren ID: %@", wareID);
    NSString *detail = [NSString stringWithFormat:@"%@", wareID];

    artikelView.productID = detail;

    [self.navigationController pushViewController:artikelView animated:YES];
    [neuheitenTableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"IndexPath: %@", [indexPath description]);
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *neuheitenCell = @"neuheitenCell";
    CJHomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:neuheitenCell];

    /*
    NSNumber *preis = [[arrayArtikelPreis objectAtIndex:indexPath.row]   objectForKey:@"preis"];
    NSMutableString  *test = [NSMutableString stringWithFormat:@"€ %@", preis];
    cell.artikelPreis.text = test;
     */

    NSString *imageString = [[arrayNeuheiten objectAtIndex:indexPath.row] objectForKey:@"bild"];
    //NSLog(@"BildURL: %@", imageString);
    NSURL *imageURL = [NSURL URLWithString:imageString];
    [cell.artikelImage setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@""]];

    //comment out wareId value assigning code here
    NSDictionary *object = [arrayNeuheiten objectAtIndex:indexPath.row];//commemted this line by mistake
    //NSLog(@"%@", object);

    //wareID = [object objectForKey:@"ware_id"];
    //NSLog(@"Waren ID: %@", wareID);

    cell.artikelName.text = [object objectForKey:@"name"];
    cell.artikelHersteller.text = [object objectForKey:@"lieferant_name"];


    return cell;

}