我在自定义CollectionView中尝试执行方法reloadData()
时遇到问题。
当解析器结束工作时,我会重新加载数据。
这是执行reloadData时的代码:
override func viewDidLoad() {
super.viewDidLoad()
SomeStructure.numeroRow = 5
self.collectionView.registerNib(UINib(nibName: "DateCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: dateCellIdentifier)
self.collectionView.registerNib(UINib(nibName: "ContentCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: contentCellIdentifier)
SomeStructure.numberRow = 200
parser.delegate = self
parser.parse {
self.collectionView.collectionViewLayout.invalidateLayout()
SomeStructure.numberRow = 190
self.collectionView ? .reloadData()
}
}
关于我在行更改大小时遇到问题,例如在此代码中我尝试加载200行的布局,并在重新加载200行布局之后。
错误是:
***断言失败 - [UICollectionViewData validateLayoutInRect:],/ SourceCache / UIKit_Sim / UIKit-3318.16.14 / UICollectionViewData.m:417
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UICollectionView接收到索引路径不存在的单元格的布局属性:{length = 2,path = 190 - 0}'
修改
cellForItemAtIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(dateCellIdentifier, forIndexPath: indexPath) as DateCollectionViewCell
dateCell.backgroundColor = UIColor.whiteColor()
dateCell.dateLabel.font = UIFont.systemFontOfSize(13)
dateCell.dateLabel.textColor = UIColor.blackColor()
dateCell.dateLabel.text = ""
return dateCell
} else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(contentCellIdentifier, forIndexPath: indexPath) as ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFontOfSize(13)
contentCell.contentLabel.textColor = UIColor.blackColor()
contentCell.contentLabel.text = "200\(indexPath.row)"
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.whiteColor()
}
println("return content cell")
return contentCell
}
} else {
if indexPath.row == 0 {
let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(dateCellIdentifier, forIndexPath: indexPath) as DateCollectionViewCell
dateCell.dateLabel.font = UIFont.systemFontOfSize(13)
dateCell.dateLabel.textColor = UIColor.blackColor()
dateCell.dateLabel.text = String("SeriesKey\nSeriesKey2")
dateCell.dateLabel.numberOfLines = 2
dateCell.dateLabel.lineBreakMode = .ByWordWrapping
if indexPath.section % 2 != 0 {
dateCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
dateCell.backgroundColor = UIColor.whiteColor()
}
return dateCell
} else {
let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(contentCellIdentifier, forIndexPath: indexPath) as ContentCollectionViewCell
contentCell.contentLabel.font = UIFont.systemFontOfSize(13)
contentCell.contentLabel.textColor = UIColor.blackColor()
contentCell.contentLabel.text = "\(indexPath.row)/\(indexPath.section)"
if indexPath.section % 2 != 0 {
contentCell.backgroundColor = UIColor(white: 242/255.0, alpha: 1.0)
} else {
contentCell.backgroundColor = UIColor.whiteColor()
}
return contentCell
}
}
}
编辑2
Method layoutAttributesForElementsInRect:
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
var attributes : NSMutableArray = NSMutableArray()
for section in self.itemAttributes {
attributes.addObjectsFromArray(
section.filteredArrayUsingPredicate(
NSPredicate(block: { (evaluatedObject, bindings) -> Bool in
return CGRectIntersectsRect(rect, evaluatedObject.frame)
})
)
)
}
return attributes
}
由于