【问题标题】:How to display Json data in UITableviewCell如何在 UITableviewCell 中显示 Json 数据
【发布时间】:2013-10-15 10:05:49
【问题描述】:

目前正在处理JSON。我只是在使用jsp webserver。并使用我想在我的uitableviewcell 中显示的 JSON 数据。我尝试了一些程序。但我遇到了一些错误。

这是我的JSON

[{"result":[{"request_id":508,"ticket_number":"PA_1000508","email":"harI","user_id":6,"description":"Feed","createdTime":"10/15/2013 10:52:32 AM","status":"initiated"},{"request_id":507,"ticket_number":"PA_1000507","email":"hari","user_id":6,"description":"","createdTime":"10/15/2013 10:52:16 AM","status":"initiated"},{"request_id":505,"ticket_number":"PA_1000505","email":"hari","user_id":6,"description":"test","createdTime":"10/15/2013 10:42:26 AM","status":"initiated"},{"request_id":504,"ticket_number":"PA_1000504","email":"hari","user_id":6,"description":"desccccccccccccccccccc","createdTime":"10/15/2013 10:42:06 AM","status":"initiated"},

我想在我的uitableviewcell中显示数据

这是我的示例代码。

TotalRequest.h

@interface TotalRequests : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableData *data;
    NSArray *listItems;
    NSURLConnection *connection;
    NSString *stat;
    IBOutlet UITableView *tableview;
}

TotalRequest.m

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"Total Requests";
    NSURL *url=[NSURL URLWithString:@"http:sampleurl"];
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
    if(connection)    
    {
        data=[[NSMutableData alloc]init];
    }
    data=[[NSMutableData alloc]init];
    [self performSelectorOnMainThread:@selector(fetchData) 
    withObject:data waitUntilDone:YES];
}

-(void)fetchData:(NSData *)responseData
{
    NSError *error=nil;
    NSDictionary *json=[NSJSONSerialization JSONObjectWithData:responseData 
    options:kNilOptions error:&error];
    NSDictionary *tRequest=[json objectForKey:@"userId"];
    NSLog(@"Total Requests :%@",tRequest);
    if(!tRequest)
    {
        NSLog(@"Error In Json :%@",error);
    }
    else
    for(NSDictionary *newValue in tRequest)
    {
        NSString *tNumber=[newValue objectForKey:@"ticket_number"];
        NSString *time=[newValue objectForKey:@"createdTime"];
        stat=[newValue objectForKey:@"status"];
        NSLog(@"ticket_number :%@",tNumber);
        NSLog(@"createdTime :%@",time);
        NSLog(@"status :%@",stat);
    }
}


- (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];
    }
    cell.textLabel.text=[[listItems objectAtIndex:indexPath.row]objectForKey:@"ticket_number"];
    cell.textLabel.text=[[listItems objectAtIndex:indexPath.row]objectForKey:@"createdTime"];
    cell.textLabel.text=[[listItems objectAtIndex:indexPath.row]objectForKey:@"status"];
    return cell;
}


-(void)connection :(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data=[[NSMutableData alloc]init];
}


-(void)connection : (NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

-(void)connection :(NSURLConnection *)connection didFailWithError:(NSError *)error

{
    NSLog(@"Error in Connection");
}

错误信息是:

-[TotalRequests fetchData]: unrecognized selector sent to instance 0x7162820 15:02:36.685 sampleWebservices[2897:11303]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TotalRequests fetchData]: unrecognized selector sent to instance 0x7162820' *** First throw call stack: (0x1c99012 0x10d6e7e 0x1d244bd 0x1c88bbc 0x1c8894e 0xb15fb4 0xb15e67 0x3d45 0xfc1c7 0xfc232 0xfc4da 0x1138e5 0x1139cb 0x113c76 0x113d71 0x11489b 0x114e93 0x114a88 0x3607 0xca285 0xca4ed 0xad45b3 0x1c58376 0x1c57e06 0x1c3fa82 0x1c3ef44 0x1c3ee1b 0x1bf37e3 0x1bf3668 0x1affc 0x28bd 0x27e5)

【问题讨论】:

  • 你的问题是什么?
  • 检查我的答案,objective-c的部分:stackoverflow.com/questions/19108976/…
  • 如果需要,使用调试器查找错误位置。这些错误是什么?
  • 我收到以下错误。如果我单击 Totalrequest 单元格,将显示以下错误消息。
  • @murthysurya 请编辑您的问题以显示错误消息。

标签: iphone ios objective-c json


【解决方案1】:

在 TotalRequest.m 中替换你的代码:

[self performSelectorOnMainThread:@selector(fetchData) withObject:data waitUntilDone:YES];

用这个:

[self performSelectorOnMainThread:@selector(fetchData:) withObject:data waitUntilDone:YES];

请注意,我在选择器(fetchData)中添加了“:”

当您使用 performSelector 方法并且您的方法接收参数时,您必须在方法名称中添加“:”。

编辑

解决您的第一个问题后,现在出现第二个问题。在您的接口声明中,您声明您实现了 UITableViewDataSource。如果你查看苹果的文档(https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html),你会看到有两个必需的方法: – tableView:cellForRowAtIndexPath: 必需的方法 – tableView:numberOfRowsInSection: 所需方法

您实现了第一个,但没有实现第二个。这是一种告诉 tableView 一个部分中有多少行的方法。我看到您的数据模型是“listItems”,因此您应该将此代码添加到您的 TotalRequest.m 文件中:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [listItems count];
}

我认为你只有一个部分。如果您有不同的需求,请调整此方法的逻辑。

【讨论】:

  • sampleWebservices[3169:11303] Json 中的错误:Error Domain=NSCocoaErrorDomain Code=3840 “操作无法完成。(Cocoa 错误 3840。)”(无值。) UserInfo=0x901d000 { NSDebugDescription=No value.} 2013-10-15 15:49:20.252 sampleWebservices[3169:11303]-[TotalRequests tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x7569c10 2013-10-15 15:49:20.274 sampleWebservices[3169 :11303] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[TotalRequests tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 0x7569c10
  • 如果我单击 Totalrequestcell,我仍然收到上述错误
  • @murthysurya 请注意,错误现在不同了。它说您正在将消息 tableView:numberOfRowsInSection: 发送到您的 TotalRequests 对象。我正在编辑我的答案来帮助你。
  • @murthysurya 你确定这是同一个错误吗?还是它现在告诉你“[TotalRequests tableView:didSelectRowAtIndexPath:]”?
  • @dude 感谢您的回复。仍然收到错误。sampleWebservices[3346:11303] Total Requests :(null) 2013-10-15 16:02:49.091 sampleWebservices[3346:11303] 错误Json :Error Domain=NSCocoaErrorDomain Code=3840 “操作无法完成。(Cocoa 错误 3840。)”(无值。) UserInfo=0x712ef90 {NSDebugDescription=无值。}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 2013-04-28
  • 2013-07-31
  • 1970-01-01
  • 2020-03-28
  • 2020-11-29
相关资源
最近更新 更多