【发布时间】:2017-04-18 05:23:46
【问题描述】:
我正在处理 JSON,在我的程序中首先调用了我的 tableView 委托方法,然后我成功地从服务器获取数据,但我想根据我们从服务器获得的记录数在 tableView 中创建行。怎么样?
我的代码是.....
- (void)viewDidLoad {
[super viewDidLoad];
self.displayDataTableView.delegate = self;
self.displayDataTableView.dataSource = self;
self.dateArray = [[NSMutableArray alloc]init];
self.amountArray = [[NSMutableArray alloc]init];
self.urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"My host name"]];
self.dataTask = [self.urlSession dataTaskWithRequest:self.urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSMutableDictionary *serverRes = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// NSLog(@"...%@ ", serverRes);
self.integer = [[serverRes objectForKey:@"Data"] count];
NSLog(@"************* = %lu", self.integer);
for (int i=0; i<self.integer; i++) {
[self.dateArray addObject:[[[serverRes objectForKey:@"Data"] objectAtIndex:i] objectForKey:@"Date"]];
[self.amountArray addObject:[[[serverRes objectForKey:@"Data"] objectAtIndex:i] objectForKey:@"TotalAmount"]];
}
NSLog(@"Date Array : %@", self.dateArray);
}];
[self.dataTask resume];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.integer;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailsCell" forIndexPath:indexPath];
return cell;
}else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailsTitle" forIndexPath:indexPath];
return cell;
}
}
谁能帮帮我.....
【问题讨论】:
-
获得数据后,只需重新加载 mainQueue 中的 tableView。
标签: ios objective-c json uitableview