dequeueReusableCellWithIdentifier有时不返回正确的原型单元格

时间:2015-06-29 00:26:06

标签: ios tableview tableviewcell

我的故事板布局中定义了一个tableView,其中定义了两个动态原型,一个用于纵向,一个用于横向。

大多数情况下表格呈现正常,但有时当我旋转设备,然后调用[tableView reloadData]时,tableViewCell将作为通用单元格而不是自定义单元格返回。

我注意到了,因为细胞是320宽,44高的标准细胞;而不是我自定义的宽度和高度。它没有定义子视图,但是当在屏幕上显示时,我的未配置的子视图就在其中。

我可以通过将reloadData移动到viewDidLoad来绕过它,但主要是在我现在旋转设备之后。

我在旋转后经过1秒的延迟后尝试做[tableView reloadData],有时会有几个返回通用,有时它们都很好。

它似乎也是以前不可见的细胞。就像横向模式显示15个单元格中的12个一样,当旋转到纵向12时,单元格很棒,但是在我重新加载数据后,最后3个单元格作为通用哑弹返回。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = @"";
    if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait ||
       [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown){
        identifier = @"tableViewCellPortrait";
    } else {
        identifier = @"tableViewCellLandscape";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    // cell is never nil, but dequeue above sometimes returns a "generic" empty cell instead of the one of the identifier...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSDictionary *data = _tableLayoutData[indexPath.row];

    // when the "generic" cell loads, all these labels come back as "nil"
    // the generic cell has no subviews, is empty
    UILabel *circleIcon = (UILabel*)[cell viewWithTag:1];
    UILabel *primaryPayerLabel = (UILabel*)[cell viewWithTag:2];
    UILabel *statusLabel = (UILabel*)[cell viewWithTag:3];
    UILabel *partySizeLabel = (UILabel*)[cell viewWithTag:4];

    // since nil, this can't be set here, and comes back as the value in the prototype once rendered
    primaryPayerLabel.text = data[@"name"];

    return cell;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [_tableView reloadData];
}

0 个答案:

没有答案
相关问题