问题描述
,目前正在使用JSON
.我只是使用jsp webserver
.我想使用JSON数据显示在uitableviewcell
中.我尝试了一些程序.但是出现了一些错误.
And am currently working on JSON
. i am just using jsp webserver
. And using JSON data i want to display in my uitableviewcell
.I tried some program.But i have getting some errors.
这是我的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)
推荐答案
在TotalRequest.m中替换您的代码:
In TotalRequest.m Replace your code:
[self performSelectorOnMainThread:@selector(fetchData) withObject:data waitUntilDone:YES];
与此:
[self performSelectorOnMainThread:@selector(fetchData:) withObject:data waitUntilDone:YES];
请注意,我在选择器(fetchData)中添加了:"
Please, note that I added ":" to selector(fetchData)
当您使用performSelector方法并且您的方法接收到一个参数时,您必须在方法名称后添加:".
When you use performSelector methods and your method receives a parameter, you have to add ":" to the method name.
编辑
解决第一个问题后,现在出现第二个问题.在接口声明中,您声明要实现UITableViewDataSource.如果您查看Apple的文档( https://developer .apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html ),您将看到有两种必需的方法:– tableView:cellForRowAtIndexPath:必需的方法– tableView:numberOfRowsInSection:必需的方法
After fixing your first problem, now appears a second one. In your interface declaration you state you implement UITableViewDataSource. If you check apple's documentation( https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html ), you will see there are two required methods:– tableView:cellForRowAtIndexPath: required method– tableView:numberOfRowsInSection: required method
您实现了第一个,但没有实现.它是一种告诉tableView一节中将有多少行的方法.我看到您的数据模型是"listItems",因此您应该将此代码添加到TotalRequest.m文件中:
You implemented the first one, but not the second. It is a method that tells a tableView how many rows there will be in a section. I see your data model is "listItems", so you should add this code to your TotalRequest.m file:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listItems count];
}
我正在考虑您只有一节.如果您有不同的需求,请调整此方法的逻辑.
I am considering you have only one section. Please adjust the logic of this method if you have different needs.
这篇关于如何在UITableviewCell中显示Json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!