Domanda

I parse an product ID via JSON which is then pushed to a detail view and put into an url to parse more information about the selected product but it seems like didSelectRowAtIndexPath returns a wrong indexPath because every time I hit a row it doesn't load the product for the detail view gets the product ID of the row which was retrieved last.

For example I have 5 rows displayed. My logic parses all ids correctly for each row (row 1, row 2, row 3, row 4, row 5). I know hit the first row because I want to get the information linked to row 1 but instead of the information of row one the detail view gets the command to parse information for row 5 because that is the last id which was parsed and then displays the wrong information.

I don't know how to make the didSelectRowAtIndexPath method get the correct information for the correct row.

Here is my Code for 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;

}

I have to make the method somehow determine which row was selected but I don't know how and couldn't find information about it in the docs.

Thanks in advance for any help!

È stato utile?

Soluzione

Hope this helps

-(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;

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top