-2

我通过 JSON 解析产品 ID,然后将其推送到详细视图并放入 url 以解析有关所选产品的更多信息,但它似乎didSelectRowAtIndexPath返回了错误的 indexPath,因为每次我点击一行时它都不会加载产品对于详细视图,获取最后检索到的行的产品 ID。

例如,我显示了 5 行。我的逻辑正确解析每一行(第 1 行、第 2 行、第 3 行、第 4 行、第 5 行)的所有 ID。我知道点击第一行,因为我想获取链接到第 1 行的信息,但不是第 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;

}

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

提前感谢您的帮助!

4

1 回答 1

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;

}
于 2013-10-25T11:31:13.297 回答