我有viewView的tableView,tableView有两个原型单元格,第二个单元格有sno,date,amount 3个标签。我创建了TableViewCell类,并在此TableViewCell类中为这3个标签创建了3个出口。我从服务器获取数据,我想将这些数据分配给这3个标签。怎么样?
在tableViewCell.h中
@property (weak, nonatomic) IBOutlet UILabel *serialNumber;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *amountLabel;
在viewController.m中
- (void)viewDidLoad {
[super viewDidLoad];
self.displayDataTableView.delegate = self;
self.displayDataTableView.dataSource = self;
self.urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://"]];
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];
dispatch_async(dispatch_get_main_queue(), ^{
[self.displayDataTableView reloadData];
});
self.dateArray = [[NSMutableArray alloc]init];
self.amountArray = [[NSMutableArray alloc]init];
[self.dateArray addObject:[[[serverRes objectForKey:@"Data"] objectAtIndex:i] objectForKey:@"Date"]];
[self.amountArray addObject:[[[serverRes objectForKey:@"Data"] objectAtIndex:i] objectForKey:@"TotalAmount"]];
}
}];
[self.dataTask resume];
}
- (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];
TableViewCell *tvc;
tvc.dateLabel.text = [self.dateArray objectAtIndex:indexPath.row];
NSLog(@"********** = %@", tvc.dateLabel.text);
return cell;
}
}
答案 0 :(得分:1)
dispatch_async(dispatch_get_main_queue(), ^{
[self.displayDataTableView reloadData];
});
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
static NSString *cellIdentifier =@"detailsCell";
// Make Your cell as this
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.dateLabel.text = [self.dateArray objectAtIndex:indexPath.row];
cell.amountLabel.text = [self.amountArray objectAtIndex:indexPath.row];
}
return cell;
}
答案 1 :(得分:0)
您需要更改tableview单元格的类
否则{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailsTitle" forIndexPath:indexPath];
cell.dateLabel.text = [self.dateArray objectAtIndex:indexPath.row];
// same
cell.amountLabel.text = [self.dateArray objectAtIndex:indexPath.row];
NSLog(@"********** = %@", cell.dateLabel.text);
return cell;
}
或更改序列
self.dateArray = [[NSMutableArray alloc]init];
self.amountArray = [[NSMutableArray alloc]init];
[self.dateArray addObject:[[[serverRes objectForKey:@"Data"] objectAtIndex:i] objectForKey:@"Date"]];
[self.amountArray addObject:[[[serverRes objectForKey:@"Data"] objectAtIndex:i] objectForKey:@"TotalAmount"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.displayDataTableView reloadData];
});
答案 2 :(得分:0)
使用此代码
self.dataTask = [self.urlSession dataTaskWithRequest:self.urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSMutableDictionary *serverRes = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.dateArray = [[NSMutableArray alloc]init];
self.amountArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [[serverRes objectForKey:@"Data"] count]; i++) {
[self.dateArray addObject:[[[serverRes objectForKey:@"Data"] objectAtIndex:i] objectForKey:@"Date"]];
[self.amountArray addObject:[[[serverRes objectForKey:@"Data"] objectAtIndex:i] objectForKey:@"TotalAmount"]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.displayDataTableView reloadData];
});
}];