【问题标题】:How to assign data from viewController to tableViewCell labels?如何将 viewController 中的数据分配给 tableViewCell 标签?
【发布时间】: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


【解决方案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; 
}

【讨论】:

  • CustomCell 在哪里 *cell = (CustomCell *) 请让您的单元格您的问题将得到解决 如果您制作自定义单元格而不是像这样制作
【解决方案2】:

您需要更改表格视图单元格的类别

其他{

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

【讨论】:

  • cell.dateLabel.text = [self.dateArray objectAtIndex:indexPath.row];表视图单元 *tvc; tvc.dateLabel.text = [self.dateArray objectAtIndex:indexPath.row];但是 tebleViewCell 中存在 dateLabel
  • 是的,但你想做什么?使用不同的类?
  • 我正在使用不同的类,即; tableViewCell 这个类有属性sno、dateLabel和amountLabel
  • 然后用我的班级改变那个班级。
【解决方案3】:

使用此代码

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

        }];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多