【问题标题】:Tableview is loading empty cells [duplicate]Tableview正在加载空单元格[重复]
【发布时间】:2020-07-05 20:43:33
【问题描述】:

我一直在尝试通过我的 uiviewcontroller 将我的 tableviewcell 链接到我在 UIView 中的 tableview。虽然 tableview 在那里,但我的自定义 tableviewcell 没有加载。日志显示单元已初始化并填充

这是我在视图控制器中使用的代码,而表视图已经在 UIView 中设置

viewcontroller.m

- (void)viewDidLoad {
    [super viewDidLoad];

    barcodeItems = [NSArray arrayWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", nil];
    NSLog(@"%@", barcodeItems);

    [photoCaptureView.itemsTableView registerNib:[UINib nibWithNibName:@"BarcodeItemsTableViewCell" bundle:nil] forCellReuseIdentifier:@"BarcodeItemsCell"];
    photoCaptureView.itemsTableView.rowHeight = 60;
    photoCaptureView.itemsTableView.dataSource = self;
    photoCaptureView.itemsTableView.delegate = self;

}

#pragma mark UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return barcodeItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Cell Initialized");
    static NSString *cellIdentifier = @"BarcodeItemsCell";

    BarcodeItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[BarcodeItemsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

//     Configure the cell...
    NSLog(@"Cell Populated");
    cell.barcodeLabel.text = @"TEST";
    UIImage *btnImage = [UIImage imageNamed:@"barcodeIcon"];
    [cell.leftButton setImage:btnImage forState:UIControlStateNormal];

    return cell;
}

【问题讨论】:

  • 您是否正确地将 tableview 的委托和数据源设置为您的视图控制器?
  • 是否调用了NSLog(@"Cell Populated");reloadData 何时调用 tableView?谁是数据源/代表(因为它是 CaptureView 的属性)
  • 只是自定义单元格没有显示还是默认 UITableViewCell 显示相同的行为?
  • 所以我只是在 viewdidload 中设置了委托和数据源。
  • 更新:日志显示单元格已初始化并填充,但显示空单元格。我提供了问题中的截图

标签: ios objective-c uitableview tableview


【解决方案1】:

我在您的代码中看不到您在 viewdidLoad 中将 Datasource 和 Delegate 设置为 self 的位置。我假设 Datasource 和 Delegate 方法与 viewdidLoad 在同一个类中。

我还注意到,您在 viewDidLoad 中初始化并将表定义为“barcodeItemsTableView”,然后在 Datasource 和 Delegate 方法中引用该表。这,据我所见,您的代码意味着表“barcodeItemsTableView”不是该类的属性,初始化表“barcodeItemsTableView”仅“存在于 viewDidLoad 中”,然后将再次被丢弃。

您应该在 view controller.h 中从您的表格中获得一个出口,或者将表格定义为一个属性并以编程方式显示它。

【讨论】:

    【解决方案2】:

    使用标识符“BarcodeItemsCell”注册自定义单元格

    [photoCaptureView.itemsTableView registerNib:[UINib nibWithNibName:@"BarcodeItemsTableViewCell" bundle:nil] forCellReuseIdentifier:@"BarcodeItemsCell"];
    

    但尝试使用名为“Cell”的标识符填充。

    static NSString *cellIdentifier = @"Cell";
    
    BarcodeItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-09
      • 2019-11-10
      • 2012-07-12
      • 1970-01-01
      • 2020-09-09
      • 2020-03-17
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多