以前的版本是beta 6,我的项目运行良好。我刚刚更新了我的xcode版本6测试版7和得到的错误,真的不知道如何修复它。
var currentcell = collectionView.cellForItemAtIndexPath(indexPath)
var posx = currentcell.frame.origin.x-collectionView.contentOffset.x
错误报告:"UICollectionViewCell?"没有名为"frame"的成员Xcode 6测试版7建议我添加?当前单元格之后。我把它改成
var posx = currentcell?.frame.origin.x-collectionView.contentOffset.x
但仍然存在错误:错误报告:可选类型"CGFloat?"的值未展开;你的意思是用"!"吗还是"?"?有人能帮忙吗?
这是正确的。?
在中的应用
currentcell?.frame.origin.x
使整个表达式成为可选的(CGFloat?
)。不能对期权进行算术运算。您必须先打开该值。
当currentCell
是nil
时,您希望posX
是什么?
可能你想做的是强制展开单元格值:
var posX = currentcell!.frame.origin.x - collectionView.contentOffset.x
在旧的贝塔中,大多数obj-c类型都是显式展开的期权(UITableViewCell!
),但其中一些被制成了纯期权(UITableViewCell?
)。注意,存在currentCell
是nil
的情况。你应该处理这些案件。
当没有为给定的indexPath返回单元格时,要避免运行时崩溃do:
if let currentcell = collectionView.cellForItemAtIndexPath(indexPath) {
var posX = currentcell.frame.origin.x - collectionView.contentOffset.x
// .. do something
}
else {
// ... there was no cell so do something else
}
试试这个:
if currentcell != nil
{
var posx = currentcell!.frame.origin.x-collectionView.contentOffset.x
}