流布局中不支持负或零大小

问题描述 投票:17回答:4

我有一个相当复杂的collectionView单元,并且我注意到,如果我在collectionView上滚动得非常快,它将使应用程序崩溃。

我得到的错误之一是:

negative or zero sizes are not supported in the flow layout

我注意到是否只返回浮点值,例如500,在我的UICollectionView sizeForItemAtIndexPath:方法中,它不会崩溃。

我的收藏夹视图具有动态的单元格高度。

我正在使用此库在sizeForItemAtIndexPath:方法中解析HTML属性字符串:

https://github.com/mmislam101/HTMLAttributedString

任何人都知道导致上述错误消息的原因是什么?

更新

我也在Bugsense报告中看到的与此崩溃有关的另一个错误是:

-[__ NSArrayM objectAtIndex:]:索引2超出范围[0 .. 1]

当我滚动太快时会发生这种情况= /

更新2

Bugsense stacktrace显示崩溃顺序方法调用为:

1)collectionView:layout:sizeForItemAtIndexPath:

2)calculateFeedCellHeightForIndexPath :(这是我自己的方法之一,而不是苹果公司的方法)

3)dynamicHeightForHTMLAttributedString:UsingWidth:AndFont :(这是我自己的方法之一,而不是Apple的方法)

4)HTMLAttributedString attributedStringWithHtml:andBodyFont:第35行

5)HTMLAttributedString attributedString第79行

6)scrollViewDidScroll:第174行

7)setFeedFooterHeight:动画:第124行

我的setFeedFooterHeight:animated:方法是:

-(void)setFeedFooterHeight:(CGFloat)newHeight animated:(BOOL)animated
{
    footerHeightConstraint.constant = newHeight;

    if(animated)
    {
        [UIView animateWithDuration:0.35 animations:^{
            [self layoutIfNeeded];  // <------ crash on this line it seems
        }];
    }
    else
    {
        [self.feedFooterView layoutIfNeeded];
    }
}

但是,当我直接从Xcode而不是上面的Testflight运行应用程序时,Xcode在上面的第5步停止,产生了这段代码:

- (NSAttributedString *)attributedString
{
    __block NSString *css       = @"<style>";

    [_cssAttributes enumerateObjectsUsingBlock:^(NSString *cssAttribute, NSUInteger idx, BOOL *stop) {
        css = [css stringByAppendingString:cssAttribute];
    }];

    css                         = [css stringByAppendingString:@"</style>"];
    NSString *htmlBody          = [_html stringByAppendingString:css];

    NSStringEncoding encoding   = NSUnicodeStringEncoding;
    NSData *data                = [htmlBody dataUsingEncoding:encoding];

    NSDictionary *options       = @{NSDocumentTypeDocumentAttribute         : NSHTMLTextDocumentType,
                                    NSCharacterEncodingDocumentAttribute    : @(encoding)};

    // app crashes here on this next line.
    NSAttributedString *body    = [[NSAttributedString alloc] initWithData:data
                                                                options:options
                                                     documentAttributes:nil
                                                                  error:nil];

    return body;
}

我在其他线程上读了一些关于包装uicollectionview重新加载直到uicollection.isTracking变为false的事情?

我尝试过,但似乎没有帮助。

更新3

[确定,我不小心发现了该错误的原因。

[collectionView.collectionFlowLayout invalidateLayout];通话有关。

我添加了1.0秒的延迟,问题似乎消失了。

ios uicollectionview autolayout uicollectionviewcell
4个回答
5
投票

我认为这是与UICollectionView的委托/数据源方法和/或NSAttributedString相关的iOS 8错误。

我可以通过在NSAttributedString中的HTML中创建一个sizeForItemAtIndexPath:,从而在一个项目中发生相同的崩溃;如果我在numberOfRows:中创建了属性字符串first

,则可以停止崩溃:所以可能是UIKit中出现了问题未正确初始化,或者HTML解析器和UICollectionView大小调整方法之一之间正在共享缓冲区。

注意:

在iOS 7.1上,我得到了一个不同的异常:“ UICollectionView收到了索引路径不存在的单元格的布局属性:”

尝试此实验:拨打类似的电话:

NSData *data = [@"<a href=\"http://www.google.com\">link</a>" dataUsingEncoding:NSUnicodeStringEncoding];
NSAttributedString *s = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}  documentAttributes:nil error:nil];

在说:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

这似乎强制初始化某些东西(也许是解析器?),这样当我在以下位置使用类似代码时:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

它不会崩溃。疯狂的小镇。

我将下载8.1 beta 2并在那里进行测试,并将报告回来。


1
投票

请确保在代码中定义collectionViewLayout.itemSize的大小不包含负/零值


0
投票

由于某些错误的计算逻辑,我发送的宽度为sizeForItemAt -6时我的应用程序崩溃了


0
投票

使用此:

© www.soinside.com 2019 - 2024. All rights reserved.