에서 다른 사람의 이름이 포함됩니다. 그러나 아래에서는 JSON 데이터를 요청하는 방법에 대한 아이디어를 얻을 것이며 JSON을 NSArray에 넣어서 UITableView에 데이터를 전달할 수 있습니다.
JSON 데이터를 가져올 위치의 URL로 URL을 설정했는지 확인하십시오.
Viewcontroller.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UITableView *UItableView;
NSArray *arrayName;
NSMutableData *dataName;
}
@end
Viewcontroller.m u는 무엇을
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Title";
NSURL *url = [NSURL URLWithString:@"URL HERE"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
dataName = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)Data
{
[dataName appendData:Data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSArray *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
if ([responseDict isKindOfClass:[NSArray class]]) {
arrayName = responseDict;
[UItableView reloadData];
} else {
NSLog(@"Error from JSON");
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayName count];
}
NSString *getStringID(id obj)
{
return [obj isKindOfClass:[NSString class]] ? obj : nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
}
cell.textLabel.text = getStringID([[arrayName objectAtIndex:indexPath.row] objectForKey:@"Label1"]);
cell.detailTextLabel.text = getStringID([[arrayName objectAtIndex:indexPath.row] objectForKey:@"Label2"]);
return cell;
}
@end
했을 ??? –
webservice에서 데이터를 가져와 섹션없이 UITable View에서 정상적으로 표시합니다. –
귀하의 요구 사항은 무엇입니까 그게 무슨 일을 했나요 ??? –