2013-10-25 50 views
-2

我通過JSON解析產品ID然後推送到一個詳細視圖並放入一個url來解析關於所選產品的更多信息,但它好像didSelectRowAtIndexPath返回一個錯誤的indexPath,因爲每次我點擊行不會加載詳細視圖的產品獲取最後檢索到的行的產品ID。didSelectRowAtIndex錯誤indexPath

例如我有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; 

} 

我必須使該方法以某種方式確定選擇哪一行,但我不知道該怎麼也找不到在該文檔中有關它的信息。

在此先感謝您的幫助!

+0

indexPath.row沒有爲你工作? –

+0

indexPath.row哪裏? –

+0

什麼是wareId在這裏?你可以發佈代碼,你正在爲wareId分配值 –

回答

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; 

} 
+0

我做了一點不同。我添加了第二個NSDictionary,因爲我需要更多的cell.artikelName.text和cell.artikelHersteller.text的對象。請爲其他人編輯您的代碼。我不想這樣做:)再次感謝! –

+0

'NSDictionary * object = [arrayNeuheiten objectAtIndex:indexPath.row];'錯誤地評論了這一行。現在請檢查一下 –

+0

是的。該代碼完全適合我! –

相關問題