【问题标题】:UITableView shows blank cells even when cellForRowAtIndexPath is called properly即使正确调用了 cellForRowAtIndexPath,UITableView 也会显示空白单元格
【发布时间】:2014-02-19 16:05:10
【问题描述】:

我有一个非常奇怪的问题,我感觉这将是一个奇怪的解决方案,就像我的配置文件有误(检查一下!)。

我可以在模拟器和我的一部 iPhone 5 上运行,并且我有一个 UITableView 填充了一些单元格,一切都很好。

但是,我刚刚在我的 5S 上运行它,并且表格视图完全空白。单元格是不可选择的或任何东西。我已经检查过 TableView 没有被释放或任何东西,如果我下拉刷新它是响应式的。我什至可以看到我的日志说 numberOfRows... 和 cellForRow.. 如果我拉刷新,方法会再次被调用,只是根本不显示 UI!

以下是各种 sn-ps,以防万一:

- (void)viewDidLoad
{
...
    UINib *cellNib = [UINib nibWithNibName:@"EJCMatchInboxCell" bundle:nil];
    [self.matchTable registerNib:cellNib forCellReuseIdentifier:kEJCMatchInboxCellIdent];

    UINib *segmentedCellNib = [UINib nibWithNibName:@"EJCSegmentedControlCell" bundle:nil];
    [self.matchTable registerNib:segmentedCellNib forCellReuseIdentifier:kEJCMatchInboxSegmentedControlCellIdent];
...
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refresh)
         forControlEvents:UIControlEventValueChanged];
    [self.matchTable addSubview:self.refreshControl];
...
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0 && indexPath.section == 0) {
        return NO;
    }

    return YES;
}

- (long)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (long)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    long rowCount = matches.count + 1;
    NSLog(@"MatchInboxVC::numberOfRowsInSection %li", rowCount);
    return rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"MatchInboxVC::cellForRowAtIndexPath %li", indexPath.row);

    if(indexPath.row == 0 && indexPath.section == 0) {
        // segmented control cell
        EJCSegmentedControlCell *cell = [tableView dequeueReusableCellWithIdentifier:kEJCMatchInboxSegmentedControlCellIdent];

        // Configure the cell...
        if (cell == nil) {
            cell = [[EJCSegmentedControlCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kEJCMatchInboxSegmentedControlCellIdent];
        }

        [cell.segmentedControl setSelectedSegmentIndex:selectedSegmentIndex];
        [cell.segmentedControl addTarget:self action:@selector(changedSegmentSelection:) forControlEvents:UIControlEventValueChanged];
        [cell.segmentedControl setTitle:@"Current" forSegmentAtIndex:0];
        [cell.segmentedControl setTitle:@"Deleted" forSegmentAtIndex:1];

        return cell;
    }
    else {
        // match cell
        EJCMatchInboxCell *cell = [tableView dequeueReusableCellWithIdentifier:kEJCMatchInboxCellIdent];

        // Configure the cell...
        if (cell == nil) {
            cell = [[EJCMatchInboxCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kEJCMatchInboxCellIdent];
        }

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        EJCMatch *m = matches[indexPath.row - 1];
        [cell setupWithMatch:m];

        return cell;
    }
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 0 && indexPath.section == 0) {
        return 38.0f;
    }
    else {
        return 65.0f;
    }
}

日志输出:

MatchInboxVC::numberOfRowsInSection 2
MatchInboxVC::cellForRowAtIndexPath 0
MatchInboxVC::cellForRowAtIndexPath 1

【问题讨论】:

  • 我刚刚注意到应用程序中的每个表格视图都发生了这种情况——它们都在 iPhone 5 和模拟器上工作,在 5S 上都没有工作???
  • 我还对模拟器进行了完全重置,以检查它没有缓存单元格的 XIB 文件或其他东西 - 它仍然在模拟器上完美运行。把5S里的app删了重新跑了还是不行。
  • 当您像这样注册 NIB 时,我认为您不需要 if(cell==nil) 签入 cellForRowAtIndexPath。保证你有一个细胞。因此,请尝试删除该 IF 语句,包括其中的内容。
  • 有趣 - 我认为 dequeueReusableCellWithIdentifier 会返回 nil ,除非回收队列中有一个实际可用的单元格?
  • 我认为这个 SO 答案很好地解释了这一点:stackoverflow.com/a/13199507/2410991

标签: ios iphone objective-c uitableview


【解决方案1】:

是的 - 我想这是我自己的错,因为忘记正确阅读方法定义,但显然在 5S 上为 heightForRowAtIndexPath 设置返回类型 (float) 将完全阻止单元格显示。

通过改变方法定义解决

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

【讨论】:

  • 非常非常奇怪,在运行 iOS7 的两台设备上的行为是不同的。刚刚检查了一下,坏掉的设备正在运行 7.0.4,而它正在运行的设备正在运行 7.0,所以他们一定在这些版本之间引入了一些东西..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多