iphone - 如何在 UITableView 中显示 NSMutableDictionary 数据数组?

标签 iphone objective-c xcode uitableview nsurlconnection

我是 iPhone 开发新手。我的应用正在发送 URL 请求以使用 NSURLConnection 从服务器检索数据。我正在获取 JSON 格式的数据,并将其作为字典数组存储在 NSMutableArray 中。所以我有一系列字典。我需要在 UITableViewCell 中显示特定数据。

我的代码在这里...

-(IBAction)ButtonTapped:(id)sender
{
     NSString *strURL=[NSString stringWithFormat:@"URl Name here"];
     NSURL *url=[NSURL URLWithString:strURL];
     self.request=[NSURLRequest requestWithURL:url]
     self.nsCon=[[NSURLConnection alloc] initWithRequest:request delegate:self];

     if(self.nsCon)
     {
        self.receivedData=[[NSMutableData alloc] init];
     }
     else
     {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Not Connected Other View !!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
        [alert show];
        [alert release];
     }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // receivedData is an instance variable declared elsewhere.
    [receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // inform the user

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",@"") message:NSLocalizedString(@"Connection failed !!",@"") delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
    [alert show];
    [alert release];

}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];     
    self.theDictionary= [[NSMutableDictionary alloc] init];
    self.theDictionary = [responseString JSONValue];

    self.arrofDictionary=[[NSMutableArray alloc] init];
     [self.arrofDictionary addObject:self.theDictionary];

    NSLog(@"data length - %@ ",[self.theDictionary objectForKey:@"today"]);

    // release the connection, and the data object
    [connection release];
    [receivedData release];

    self.tblList=[[UITableView alloc] initWithFrame:CGRectMake(0,80,320,370) style:UITableViewStyleGrouped];
    self.tblList.delegate=self;
    self.tblList.dataSource=self;
    [self.view addSubview:self.tblList];

}

//consol中的数据展示

(
        {
        "date_time" = "2012-08-31 09:30:25";
        id = 99;
        language = en;
        name = Tt;
        score = 656;
    },
        {
        "date_time" = "2012-08-31 10:08:52";
        id = 103;
        language = en;
        name = Fg;
        score = 567;
    },
        {
        "date_time" = "2012-08-31 08:22:38";
        id = 87;
        language = en;
        name = Eree;
        score = 565;
    },
)

那么如何在UITableViewCell 上显示namescore ??

最佳答案

您显示的数据位于字典对象数组 (yourArray) 中。您使用以下代码。

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

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }
     cell.textLabel.text = [[yourArray objectAtIndex:indexPath.row]  objectForKey:@"name"];
    cell.detailTextLabel.text = [[yourArray objectAtIndex:indexPath.row]  objectForKey:@"score"];



    return cell;
}

我想这对你有帮助。

关于iphone - 如何在 UITableView 中显示 NSMutableDictionary 数据数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12213351/

相关文章:

iPhone:创建类似 Facebook 的 UI、皮肤应用程序,不遵循 AHIG

ios - Whatsapp 消息布局 - 时间和消息在同一行

objective-c - 优雅地替换 URL/NSString 中的多个标记

iphone - Facebook SDK 3.0 授权后启动 View Controller

ios - 在 iOS 设备上运行时,相机预览不符合 UIView 边界

iphone - 当方向改变时如何管理应用程序?

iphone - "Object 0x84be00 of class NSCFString autoreleased with no pool in place - just leaking"- 但在应用程序的第一行!

ios - 当前 View Controller 后 subview 重置为原始框架

iphone - UITextView inside UIScrollView 滚动问题

ios - 导航 Controller 中嵌入的 View 中的 UINavigationBar - 添加自定义 View