uiCollectionViewCell 内的 Uitableview

Posted

技术标签:

【中文标题】uiCollectionViewCell 内的 Uitableview【英文标题】:Tableview inside a CollectionViewCell 【发布时间】:2013-12-10 03:22:02 【问题描述】:

有没有人有在 collectionViewCell 中使用 tableView 的经验?

我在 collectionViewCell 中有一个 tableview(每个都是全屏视图)。每个 collectionViewCell 都显示自己的独特数据。您可以在每个 collectionViewCell 之间水平滚动。 tableview 有一个带有几个标签的自定义 tableviewCell。我正在使用 tableview 来利用内置的 tableview 重新排序功能。

第一个 collectionViewCell 加载正常,数据正确显示在 tableview 中。但是,当我在 collectionView 单元格之间滚动时,tableView 为空(在标签中仅显示占位符文本),直到 collectionViewCell 停止滚动,然后 tableview 突然出现正确的数据。

关于如何确保表格视图在显示之前已填充正确数据的任何建议?当用户拖动下一个 collectionViewCell 时,我需要它已经存在(就像背景图像/标题文本已经存在一样)。

Here's a link to my project so you can see the code.

任何帮助表示赞赏!

【问题讨论】:

【参考方案1】:

我没有这样做的经验。

但我认为您可以减少创建对单元格的引用的问题。所以你的细胞不会被释放。您可以使用数组来执行此操作。

编辑:

您的问题是您正在使用计时器来更新TableViewCell 中的标签

你可以改变这个:

if (!timer) 
        timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(updateLabels:) userInfo:nil repeats:YES];
    

为此:

[self updateLabels:nil];

并为此更改您的updateLabels:

- (void)updateLabels:(NSTimer *)_timer

    if (!timer) 
        timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(updateLabels:) userInfo:nil repeats:YES];
    

    unitOfTimeLabel.text = nameOfTimeLabel;

    if ([nameOfTimeLabel isEqualToString:@"Days"]) amountOfTimeLabel.text = [self daysRemaining];
    if ([nameOfTimeLabel isEqualToString:@"Hours"]) amountOfTimeLabel.text = [self hoursRemaining];
    if ([nameOfTimeLabel isEqualToString:@"Minutes"]) amountOfTimeLabel.text = [self minutesRemaining];
    if ([nameOfTimeLabel isEqualToString:@"Seconds"]) amountOfTimeLabel.text = [self secondsRemaining];

【讨论】:

你指的是collectionViewCell还是tableviewCell?我需要重用 collectionViewCells,因为它们可能有很多,如果可能的话,我不想将它们保留在内存中。 我指的是collectionViewCell。但是如果你有很多,你需要寻找另一种方法,也许有一个加载指示器。我不知道你怎么能在集合显示之前等待reloadTable... dcorbatta 你是个传奇!仍在尝试了解这是如何修复它的,但它可以完美运行。感激不尽。 我想我可以解释一下为什么这可以解决问题。因为 Timer 在当前循环完成时运行,所以我的意思是计时器被安排以零延迟运行,但在循环完成时运行。另一方面,如果您在同一个循环中调用该方法,则可以确保它们在同一个循环中运行。 这是有道理的。我刚刚注意到,当我们在 collectionViewCells 之间滚动时,内存使用量不断增加,我猜是因为 tableview 单元格没有出列,而是通过 nib 不断加载。你知道解决这个问题的方法吗?【参考方案2】:

在collectionviewcontroller 中替换这个方法。在这里我们可以动态设置单元格的值

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

    tableViewTimeDisplayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell == nil) 

        NSArray *arrayCellXib = [[NSBundle mainBundle] loadNibNamed:@"tableViewTimeDisplayCell"
                                                              owner:self
                                                            options:nil];

        cell = arrayCellXib[0];//[arrayCellXib objectAtIndex:0];
        [cell setNameOfTimeLabel:cellOrderArray[indexPath.row]];//[cellOrderArray objectAtIndex:1] here we can make it as dynamic index
        [cell setEventNumber:eventNumber];
        [cell startTimer];
    

    return cell;

// 我想问题是当我们水平滚动时,Tableview 再次分配了集合视图。在集合视图中,我删除了除重新加载 tableview 之外的代码并粘贴在 initwithframe 中。由于每次发生这种情况时视图都会分配。

【讨论】:

嘿,谢谢。当然更整洁的代码。你能解释一下关于在 initwithframe 中粘贴 reload tableview 的最后声明吗?是的,每个 tableview 分配都会占用内存。需要解决这个问题。

以上是关于uiCollectionViewCell 内的 Uitableview的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionViewCell 内的圆形 ImageView

UICollectionViewCell 内的 UIImageView 中的角半径

从 uiviewcontroller 操作 uicollectionviewcell 内的 uicollectionview

在 UICollectionViewCell 内的 UICollectionView 中平滑滚动

uiCollectionViewCell 内的 Uitableview

Swift - 从 UICollectionViewCell 内的视图中删除约束