我正在研究JSON,在我的程序中我的tableView委托方法首先调用,然后我成功从服务器获取数据,但我想根据从服务器获取的记录数量在tableView中创建行。怎么样?
我的代码是.....
var server = new Hapi.Server()
global.configurationManagerObj = 'Dev'
任何人都可以帮忙.....
答案 0 :(得分:1)
您需要检查空单元格并使用相同的标识符创建新单元格
if(indexPath.row == 0)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailsCell" forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detailsCell"];
}
return cell;
}else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailsTitle" forIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detailsTitle"];
}
return cell;
}
或从服务器
获取数据时重新加载tableview 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"]];
}
[tblview reloadData];
答案 1 :(得分:1)
UITableView
,因此您需要在从服务器获取结果后重新加载UITableView
。
self.integer = [[serverRes objectForKey:@"Data"] count];
NSLog(@"************* = %lu", self.integer);
dispatch_async(dispatch_get_main_queue(), ^{
[self.displayDataTableView reloadData];
});
这对我来说是正确的解决方案......
答案 2 :(得分:1)
使用此功能。
- (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:@"API 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);
dispatch_async(dispatch_get_main_queue(), ^{
[self.displayDataTableView reloadData];
});
}];
[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;
}
}