array返回null值,arr = [[tempDict1 valueForKey:@“rates”] componentsSeparatedByString:@“;”];

时间:2012-09-13 13:44:48

标签: iphone ios json

我在NSArray中显示UITableView值时遇到问题。在我的代码中,我得到nil值。

arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"];

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
      [connection release];

       NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
       self.responseData = nil;

       values = [responseString JSONValue];
       array = [[NSMutableArray alloc] init];
       NSMutableArray *arrTitle = [[NSMutableArray alloc] init];
       NSMutableArray *arrValues = [[NSMutableArray alloc] init];
       array =[values valueForKey:@"rates"];

       NSLog(@"array values:--> %@",array);
 //    NSLog(@"values:--> %@",values);
 //    NSLog(@"Particular values:--> %@",[[values valueForKey:@"rates"] valueForKey:@"AED"]);

       tempDict1 = (NSMutableDictionary *)array;            
       NSArray *arr;// =[[NSArray alloc]init];
       arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"];
       NSLog(@"arr-->%@",arr);
       NSString *subStar = @"=";
       [arrTitle removeAllObjects];
       [arrValues removeAllObjects];

       for (int i=0; i<[arr count]-1; i++)
       {
            [arrTitle addObject:[[arr objectAtIndex:i] substringToIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])-1]];
            [arrValues addObject:[[arr objectAtIndex:i] substringFromIndex:NSMaxRange([[arr objectAtIndex:i] rangeOfString:subStar])]];
             NSLog(@"arrTitle is:--> %@",arrTitle);
       }

       tempDict1 = (NSMutableDictionary*)[array objectAtIndex:0];
       array = [values valueForKey:@"rates"];
       NSLog(@"tempDict--%@",[tempDict1 objectForKey:@"AED"]);

       [array retain];
       [tbl_withData reloadData];
}

uitableview代码位于

之下
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"array-->%@",array);
    return [array count];

}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    intIndexPath = indexPath.row;
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
        cell.textLabel.font = [UIFont systemFontOfSize:8];
        cell.textLabel.numberOfLines = 4;

    }

//    NSLog(@"data is like:--> %@",array);
//    cell.textLabel.text= [NSString stringWithFormat:@"%@",[array objectAtIndex:intIndexPath]];
    cell.textLabel.text =[array objectAtIndex:intIndexPath];

    return cell;
}

1 个答案:

答案 0 :(得分:1)

查看表视图编程指南

https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html

您必须在类中实现UITableView委托方法,以使用内容填充表视图。

另见: https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10

这是代码:

    //this is your dictionary:
    self.values = [[[NSMutableDictionary alloc] init] autorelease];
    //filled it with values:
    [self.values setObject:@"201" forKey:@"ams"];
    [self.values setObject:@"500.50" forKey:@"hyd"];
    [self.values setObject:@"200.10" forKey:@"guj"];
    [self.values setObject:@"400" forKey:@"afgd"];
    //now get an array of keys out of it. Can do it anywehe... For example in connectionDidFinishLoading method
    self.keys = [[[NSArray alloc] initWithArray:[values allKeys]] autorelease];

现在是cellForRoAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.adjustsFontSizeToFitWidth = YES;
        cell.textLabel.font = [UIFont systemFontOfSize:8];
        cell.textLabel.numberOfLines = 4;

    }
    NSString *currentKey = [self.keys objectAtIndex:indexPath.row];
    NSString *currentValue = [self.values objectForKey:currentKey];

    NSString *cellText = [NSString stringWithFormat:@"%@:%@", currentKey, currentValue];

    cell.textLabel.text = cellText;

    return cell;
}

它在我的机器上工作..