Zha*_*ang 15 ios autolayout uicollectionview uicollectionviewcell
我有一个相当复杂的collectionView单元格,我注意到如果我在我的collectionView上滚动得非常快,它会崩溃应用程序.
我得到的错误之一是:
negative or zero sizes are not supported in the flow layout
Run Code Online (Sandbox Code Playgroud)
我注意到如果我只是返回一个浮点值,例如500,在我的UICollectionView sizeForItemAtIndexPath:方法中,它不会崩溃.
我的集合视图具有动态单元格高度.
我正在sizeForItemAtIndexPath:使用此库在我的方法中解析HTML Attributed字符串:
https://github.com/mmislam101/HTMLAttributedString
任何人都知道是什么原因导致上述错误消息特别发生?
我在Bugsense报告中看到的与此崩溃有关的另一个错误是:
- [__ NSArrayM objectAtIndex:]:索引2超出边界[0 .. 1]
当我滚动得太快时会发生这种情况= /
Bugsense stacktrace显示崩溃顺序方法调用是:
1)collectionView:layout:sizeForItemAtIndexPath:
2)calculateFeedCellHeightForIndexPath :(这是我自己的方法之一,而不是Apple的方法)
3)dynamicHeightForHTMLAttributedString:UsingWidth:AndFont :(这是我自己的方法之一,而不是Apple的方法)
4)HTMLAttributedString attributionStringWithHtml:andBodyFont:第35行
5)HTMLAttributedString attributionString第79行
6)scrollViewDidScroll:第174行
7)setFeedFooterHeight:animated:第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];
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我直接从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;
}
Run Code Online (Sandbox Code Playgroud)
我在其他主题上阅读了关于包装uicollectionview重新加载的内容,直到uicollection.isTracking变为false?
我尝试过,但似乎没有帮助.
好吧,我不小心偶然发现了那个错误的原因.
这与[collectionView.collectionFlowLayout invalidateLayout];电话有关.
我添加了1.0秒的延迟,问题似乎消失了.
我认为这是与 UICollectionView 的委托/数据源方法和/或 NSAttributedString 相关的 iOS 8 错误。
我可以通过创建一个获得在一个项目中此相同的崩溃NSAttributedString从HTML中sizeForItemAtIndexPath:如果我创建一个属性串首先在numberOfRows:此停止崩溃:所以有可能的问题是什么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];
Run Code Online (Sandbox Code Playgroud)
在,说:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)
这似乎强制初始化某些东西(也许是解析器?),这样当我在以下位置使用类似的代码时:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
它不会崩溃。疯狂的小镇。
我将下载 8.1 beta 2 并在那里进行测试,然后会回来报告。
| 归档时间: |
|
| 查看次数: |
14116 次 |
| 最近记录: |