从ios UITABLEVIEW打印特定值

时间:2012-12-13 16:29:46

标签: ios6

SOffer[26229:c07] current Product: (
    {
        id = 20;
        image = "img2.png";
        name = "offer 2";
    }
)

当我通过NSLog打印时,我有产品导致上述结果,我需要单独打印这些产品。以下代码生成此

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"cell"];
        }
        NSString *currentProductName;
        currentProductName = [productKeys objectAtIndex:indexPath.row];

        currentProduct = [products objectForKey:[productKeys objectAtIndex:indexPath.row]];
        NSLog(@"current Product: %@",currentProduct);
        //currentProductName = [currentProduct objectForKey:@"image"];
        //currentProduct = [currentProduct ];

        [[cell textLabel] setText:currentProductName];

    return cell;
    } 

currentProduct被声明为NSArray,但是当我尝试使用objectForKey进行打印时,它说NSArray的可见接口没有声明选择器ObjectForKey

1 个答案:

答案 0 :(得分:1)

objectForKey,我认为仅适用于NSDictionary

NSArray没有键值对。除非currentProduct包含JSON文件,否则您必须首先获取第一个产品的Array对象并将其分配给NSDictionary,然后您可以使用objectForKey获取你的形象价值。

这样的事情:

// Get the first product object from the NSArray. Assuming you dropped your entire Product values into a NSArray
NSMutableDictionary *prodDict = [currentProduct objectAtIndex:indexPath.section];
NSString *prodID = [prodDict objectForKey:@"id"];
NSString *prodName = [prodDict objectForKey:@"name"];
...