gpt4 book ai didi

ios - 如何将数据从 viewController 分配给 tableViewCell 标签?

转载 作者:行者123 更新时间:2023-11-29 00:24:19 25 4
gpt4 key购买 nike

我有带 tableView 的 viewController,tableView 有两个原型(prototype)单元格,第二个单元格有 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;


}

}

最佳答案

 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;
}

关于ios - 如何将数据从 viewController 分配给 tableViewCell 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43465556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com