2013-10-15 64 views
-1

而我目前正在致力於JSON。我只是使用jsp webserver。並使用JSON數據我想要顯示在我的uitableviewcell。我嘗試了一些program.But我有一些錯誤。如何在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. 
    [email protected]"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) 
+2

你的問題是什麼? – user1673099

+0

檢查我的答案,目標C的部分:http://stackoverflow.com/questions/19108976/ios-objective-c-how-do-i-query-a-web-server-database-from-an- iphone-app/19110548#19110548 – Ilario

+0

如果需要,使用調試器查找錯誤的位置。這些錯誤是什麼? –

回答

3

在TotalRequest.m替換代碼:

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

與此:

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

請注意,我說「:」選擇器(fetchData)

當您使用performSelector方法並且您的方法接收到參數時,必須將「:」添加到方法名稱。

編輯

修理你的第一個問題後,現在出現第二個。在你的接口聲明中,你聲明你實現了UITableViewDataSource。如果您檢查Apple的文檔(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]; 
} 

我考慮您只有一個部分。如果您有不同的需求,請調整此方法的邏輯。

+0

sampleWebservices [3169:11303] Json中的錯誤:錯誤域= NSCocoaErrorDomain代碼= 3840「操作無法完成(可可錯誤3840.)」(No值)。UserInfo = 0x901d000 {NSDebugDescription =無值} sampleWebservices [3169:11303] - [TotalRequests tableView:numberOfRowsInSection:]:無法識別的選擇器發送到實例0x7569c10 2013-10- 15 15:49:20。274 sampleWebservices [3169:11303] ***由於未捕獲異常'NSInvalidArgumentException',原因:' - [TotalRequests tableView:numberOfRowsInSection:]:無法識別的選擇器發送到實例0x7569c10 –

+0

仍然我得到上述錯誤,如果我點擊Totalrequestcell –

+0

@murthysurya請注意現在的錯誤是不同的。它說你正在向你的TotalRequests對象發送消息tableView:numberOfRowsInSection:。我在編輯我的答案來幫助你。 –