【发布时间】:2014-12-02 16:11:55
【问题描述】:
我有一个相当复杂的 collectionView 单元格,我注意到如果我在 collectionView 上滚动得非常快,它会使应用程序崩溃。
我得到的错误之一是:
negative or zero sizes are not supported in the flow layout
我注意到如果我只返回一个浮点值,例如500,在我的 UICollectionView sizeForItemAtIndexPath: 方法中,它不会崩溃。
我的收藏视图具有动态单元格高度。
我正在使用这个库在我的 sizeForItemAtIndexPath: 方法中解析 HTML Attributed 字符串:
https://github.com/mmislam101/HTMLAttributedString
有谁知道具体是什么原因导致出现上述错误消息?
更新
我在 Bugsense 报告中还看到与此崩溃相关的另一个错误是:
-[__NSArrayM objectAtIndex:]:索引2超出范围[0 .. 1]
当我滚动太快时会发生这种情况 = /
更新 2
Bugsense 堆栈跟踪显示崩溃顺序方法调用是:
1) collectionView:layout:sizeForItemAtIndexPath:
2)calculateFeedCellHeightForIndexPath:(这是我自己的方法之一,不是苹果的)
3) dynamicHeightForHTMLAttributedString:UsingWidth:AndFont:(这是我自己的方法之一,不是苹果的)
4) HTMLAttributedString attributesStringWithHtml:andBodyFont: 第 35 行
5) HTMLAttributedString 属性字符串第 79 行
6) scrollViewDidScroll:第 174 行
7) setFeedFooterHeight:animated: line 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 秒的延迟,问题似乎消失了。
【问题讨论】:
-
那么您的代码是否曾经返回零/负高度?什么时候?
-
我尝试在 sizeForItemAtIndexPath 的返回语句之前执行 if(returnedHeight == 0) { NSLog(@"height is 0"; },但它永远不会到达该代码断点。
-
不检查是否与零相等,检查
< 1 -
在 Dropbox 中分享您的代码,以便我们了解更多详情?
-
T-T 我希望我可以,但这是公司不想披露的敏感信息。看来我一个人了。尽管如此,我还是感谢您的帮助。
标签: ios uicollectionview autolayout uicollectionviewcell