简体   繁体   English

将scrollOffset保存到didEndDisplayingCell中的UITableView数据源,而不是正确出列

[英]Saving scrollOffset to UITableView datasource in didEndDisplayingCell not dequeuing properly

I've got a UITableView with a custom cell defined, and in each custom cell, is a UICollectionView. 我有一个定义了自定义单元格的UITableView,并且在每个自定义单元格中都是一个UICollectionView。 I'm populating the data from a top-level array of data objects, with each containing an inner array of data objects for the collection view. 我正在从顶级数据对象数组中填充数据,每个数据对象都包含集合视图的内部数据对象数组。 In JSON, this looks something like: 在JSON中,这看起来像:

[
    {
        "collectionViewData": [
            {
                "name": "subobject1"
            },
            {
                "name": "subobject2"
            },
            {
                "name": "subobject3"
            }
        ],
        "name": "object1"
    },
    {
        "collectionViewData": [
            {
                "name": "subobject1"
            },
            {
                "name": "subobject2"
            }
        ],
        "name": "object2"
    },
    {
        "collectionViewData": [
            {
                "name": "subobject1"
            },
            {
                "name": "subobject2"
            },
            {
                "name": "subobject3"
            },
            {
                "name": "subobject4"
            }
        ],
        "name": "object3"
    }
]

This works fine, and behaves as expected. 这工作正常,并按预期行事。 What I'm trying to do now is save the contentOffset on didEndDisplayingCell into my datasource so that the next time the tableview cell is redrawn, the scroll location is restored. 我现在要做的是将didEndDisplayingCell上的contentOffset保存到我的数据源中,以便下次重绘tableview单元格时,滚动位置将被恢复。

This seems to work just fine - except something is goofing in the dequeuing mechanism because every other cell is loading the previously dequeued cell's offset - eg: 这似乎工作得很好 - 除了在出列机制中出现问题,因为每个其他单元格都在加载先前出列的单元格的偏移量 - 例如:

  • 0 = offset of {170, 0} 0 = {170,0}的偏移
  • 1 = offset of {0, 0} 1 = {0,0}的偏移
  • 2 then loads with 0's offset of {170, 0} 2然后加载0的偏移量{170,0}

What's going wrong? 出了什么问题? Why am I seeing this behavior and how do I fix it? 为什么我会看到此行为以及如何解决此问题?

- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Identifier";

    MyTableViewCell *row = (MyTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (row == nil)
    {
        row = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    NSInteger idx = (indexPath.row % [MUTABLE_DATASOURCE_ARRAY count]);
    MyCustomCollectionObjectContainer *collectionViewObjectContainer = [MUTABLE_DATASOURCE_ARRAY objectAtIndex:idx];

    [row.collectionView setContentOffset:collectionViewObjectContainer.lastContentOffset animated:NO];
    DDLogDebug(@"ROW %ld %@: cellForRow actual: %@, want: %@", idx, collectionViewObjectContainer.collectionName, NSStringFromCGPoint(row.collectionView.contentOffset), NSStringFromCGPoint(collectionViewObjectContainer.lastContentOffset));
    row.collectionName = collectionViewObjectContainer.collectionName;
    row.collectionViewObjectContainer = collectionViewObjectContainer.innerDataArray;
    row.labelcollectionViewObjectContainer.text = [collectionViewObjectContainer.collectionName uppercaseString];
    [row.labelcollectionViewObjectContainer setNeedsDisplay];

    return row;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(MyTableViewCell *)row forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger idx = (indexPath.row % [MUTABLE_DATASOURCE_ARRAY count]);
    MyCustomCollectionObjectContainer *collectionViewObjectContainer = [MUTABLE_DATASOURCE_ARRAY objectAtIndex:idx];
    collectionViewObjectContainer.lastContentOffset = row.collectionView.contentOffset;
    MUTABLE_DATASOURCE_ARRAY[idx] = collectionViewObjectContainer;
    DDLogDebug(@"ROW %ld %@: didEndDisplayingCell set: %@", idx, collectionViewObjectContainer.collectionName, NSStringFromCGPoint(collectionViewObjectContainer.lastContentOffset));
}

Update: In this log output, sometimes the "actual" is wrong, and the "want" is correct, even though in the previous line, I'm clearly setting the content offset. 更新:在此日志输出中,有时“实际”是错误的,并且“想要”是正确的,即使在上一行中,我显然正在设置内容偏移。 Is there a quick and dirty way to force the content offset to take? 是否有一种快速而肮脏的方式来强制内容偏移量?

[row.collectionView setContentOffset:collectionViewObjectContainer.lastContentOffset animated:NO];
DDLogDebug(@"ROW %ld %@: cellForRow actual: %@, want: %@", idx, collectionViewObjectContainer.collectionName, NSStringFromCGPoint(row.collectionView.contentOffset), NSStringFromCGPoint(collectionViewObjectContainer.lastContentOffset));

Further to this, I forgot (slap💥!) that it wasn't working at all in the first place, so I overrode setContentOffset in my collection view subclass: 除此之外,我忘了(slap💥!)它首先没有工作,所以我在我的集​​合视图子类中覆盖了setContentOffset

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
{

}

Doh! 卫生署! That doesn't do ANYTHING. 这没有做任何事情。 But if I remove it, now I'm plagued with my original problem in that when the new table view cell is initialized, the inner collection view does NOT scroll to the desired content offset even though the logging correctly logs out the content offset each time now. 但是,如果我删除它,现在我困扰我的原始问题,当初始化新的表视图单元格时,内部集合视图不会滚动到所需的内容偏移量,即使每次正确记录日志记录的内容偏移量现在。 Maybe I need to call something other than setContentOffset altogether? 也许我需要完全调用setContentOffset以外的东西?

Super messed up, but it seems to work just fine when I put a 0.1s delay before calling setContentOffset : 超级搞砸了,但是在调用setContentOffset之前我放了0.1秒延迟似乎工作得很好:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [row.collectionView setContentOffset: collectionViewObjectContainer.lastContentOffset animated:NO];
});

Seems like this is more of a hack than a solution, but it is indeed working. 看起来这更像是一个黑客而不是一个解决方案,但它确实有效。

Update: 更新:

Tragic end to what could have been a cool problem to solve. 悲惨的结局可能是一个很难解决的问题。 Turns out I was setting the content offset in my UITableViewCell subclass's awakeFromNib to CGPointZero. 事实证明我在UITableViewCell子类的awakeFromNib中将内容偏移设置为CGPointZero。 Setting it to my saved content offset fixed it. 将其设置为我保存的内容偏移量固定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM