【问题标题】:UITableViewCells not appearing initiallyUITableViewCells 最初没有出现
【发布时间】:2012-01-25 21:50:14
【问题描述】:

我有三个应该看起来相同的 UITableView。每个都放置在 UIScrollView 的不同子视图中。当我滚动时,我可以看到每个表。第一张桌子看起来很完美。但是,第二个和第三个表格只显示背景颜色。

tableView:cellForRowAtIndexPath: 正在为每个表的每一行调用,我在单元格的 contentView 中插入一个子视图。在调用此方法之前已经创建了子视图,并在给定行的三个表中重复使用(以使表看起来相同)。

我有两个有趣的发现:

  • 虽然单元格最初不显示,但在滚动隐藏行后会出现,然后再次显示。
  • 当单元格在滚动后出现时,它已从另一个表格中移出。它不再出现在它之前显示的表格中。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"PlotCellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PlotCell" owner:self options:nil];

        if ([nib count] > 0)
            cell = self.plotCell;
        else
            NSLog(@"Failed to load AlertCell nib!");
    }

    // Set up the cell...
    NSUInteger row = [indexPath row];
    AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    ResultSet *rs = del.resultPlots;

    CPTGraphHostingView *cellPlotView = [rs.hostingViews objectAtIndex:row];
    cellPlotView.frame = cell.contentView.bounds;

    cellPlotView.autoresizingMask = UIViewAutoresizingFlexibleWidth;    
    [cell.contentView insertSubview:cellPlotView atIndex:0];

    return cell;
}

cellPlotView 指的是已经创建的子视图。

知道如何解决这个问题,以便在滚动时看到所有表格正确显示吗?就好像单元格不能重用内容一样。谢谢。

【问题讨论】:

  • 三个表视图是否使用相同的实现?
  • 是的,它们使用相同的实现
  • 视图只能有一个超级视图。所以创建 3 个相同的 CPTGraphHostingView 视图。
  • 您不需要根据row 值计算正确的数组索引吗?我的意思是每个表的第一行的单元格每次都不是源数组索引 0,是吗?
  • @Vince 每个表的第一行总是使用索引 0

标签: ios cocoa-touch uitableview uiscrollview


【解决方案1】:

视图只能有一个超级视图。所以创建 3 个相同的 CPTGraphHostingView 视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多