关于 ios:CollectionViewCell 中的 Tableview

Tableview inside a CollectionViewCell

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

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

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

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

这是我的项目的链接,您可以查看代码。

任何帮助表示赞赏!


在collectionviewcontroller 中将此方法替换为this。在这里我们可以动态设置值到 cell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-(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 中。由于每次发生这种情况时视图都会分配。


我没有这样做的经验。

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

编辑:

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

中的标签

你可以改变这个:

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

为此:

1
[self updateLabels:nil];

并为此更改您的 updateLabels:

1
2
3
4
5
6
7
8
9
10
11
12
13
- (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];
}