Я пытаюсь загрузить json в uitableview. Я работал с json раньше, но никогда не использовал его с tableview. Я продолжаю получать эту ошибку: - [__ NSCFNumber count]: непризнанный селектор, отправленный в экземпляр. Я уверен, что это потому, что в методе numberOfRowsInSection im возвращается счетчик массива. Пожалуйста, дайте мне знать, как это исправить, или если я что-то упускаю и не вижу этого.Загрузка Json в uitableview
Вот он код: .h файл
@interface HistoryViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
NSArray *jsonData;
NSMutableData *responseData;
}
.m файл
- (void)viewDidLoad
{
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"Json url"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
[super viewDidLoad];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *dictionary = [responseString JSONValue];
NSArray *response = [dictionary objectForKey:@"name"];
jsonData = [[NSArray alloc] initWithArray:response];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return jsonData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
cell.textLabel.text = [jsonData objectAtIndex:indexPath.row];
return cell;
}
Я предполагаю, что '[dictionary objectForKey: @" name "]' фактически возвращает число, а не массив, подобный вашему коду. Не могли бы вы разместить строку с 'NSLog (@" Key:% @ ", [dictionary objectForKey: @" name "])' или что-то в этом роде и сообщить нам, что это возвращает? Это позволит нам выяснить, какой тип данных находится в этом словаре. –
Вы должны использовать AFNetwork + JSONKit и использовать AFJsonRequestOperation, это намного проще! – Cyrille
@Paul Я вставил NSLog, и он возвращает массив – Sean