【发布时间】:2017-04-18 06:55:30
【问题描述】:
我有带tableView 的viewController,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;
}
}
【问题讨论】:
-
我在 viewController 中导入了 tableViewCell .h 并分配了 TableViewCell *tvc; tvc.dateLabel.text = [self.dateArray objectAtIndex:indexPath.row]; NSLog(@"********** = %@", tvc.dateLabel.text);但我显示为空
-
是的,它的值为NULL,它没有在tableView中打印任何数据
标签: ios objective-c uitableview uiviewcontroller