我试图实现一个Collection View控制器并且大量失败......我如何摆脱这个解缠错误?当我在cellForRowAtIndexPath函数中创建collectionView单元时,会发生这种情况。
这是代码。
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = picturesCollectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as? PhotoBrowserCollectionViewCell
if let photoObject = pictureObjects.objectAtIndex(indexPath.row) as? PictureElement {
let title = photoObject.title ?? "" // if photoObject.title == nil, then we return an empty string
let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(photoObject.timeStamp))
let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)
let author = photoObject.author ?? ""
let issueNumber = photoObject.issueNumber ?? ""
let volumeNumber = photoObject.volumeNumber ?? ""
let nodeID = photoObject.nodeID ?? 0
let imageURL = photoObject.imageURL ?? ""
cell!.request?.cancel()
// Using image cache system to make sure the table view works even when rapidly scrolling down the screen.
if let image = self.imageCache.objectForKey(imageURL) as? UIImage {
cell!.imageView.image = image
} else {
cell!.imageView.image = nil
cell!.request = Alamofire.request(.GET, imageURL).responseImage { response in
if let image = response.result.value {
self.imageCache.setObject(response.result.value!, forKey: imageURL)
cell!.imageView.image = image
} else {
}
}
}
} else {
}
return cell!
}
另外,我已经尝试过使用一个guard-let语句而且它没有用。这是:
guard let cell = picturesCollectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as? PhotoBrowserCollectionViewCell else {
print("cell could not be initialized as PhotoBrowserCollectionViewCell, hence it will be casted to another default UICollectionViewCell type")
return collectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath)
}
答案 0 :(得分:0)
假设您正在使用故事板......
确保为故事板中的单元格设置重用标识符以匹配PhotoBrowserCellIdentifier的值。
确保将故事板中的单元格类设置为PhotoBrowserCollectionViewCell。