func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == thisSeasonCollectionView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell
let imageView = cell.viewWithTag(1) as! UIImageView
let url = NSURL(string: URLArrayStringThisSeason[indexPath.row])
let placeholderImage = UIImage(named: "Rectangle")!
let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
size: imageView.frame.size,
radius: 0
)
imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
)
cell.layer.cornerRadius = 3.0
return cell
}
else if collectionView == whatsNewCollectionView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell
let imageView = cell.viewWithTag(1) as! UIImageView
let url = NSURL(string: URLArrayStringRecents[indexPath.row])
let placeholderImage = UIImage(named: "Rectangle")!
let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
size: imageView.frame.size,
radius: 0
)
imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
)
cell.layer.cornerRadius = 3.0
return cell
}
}
为什么这不起作用?我想尝试将3个集合视图从不同的tableView Cell链接到这个swift文件,但似乎只有两个。如果我因为某些原因将'else if'替换为'else',代码工作正常。
修改:
如何编辑细胞计数的返回值:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == thisSeasonCollectionView {
return URLArrayStringThisSeason.count
}else if collectionView == whatsNewCollectionView {
return URLArrayStringRecents.count
}else if collectionView == labelCollectionView {
return URLArrayStringLabel.count
}
}
答案 0 :(得分:1)
将您的代码更改为以下内容:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell
if collectionView == thisSeasonCollectionView {
let imageView = cell.viewWithTag(1) as! UIImageView
let url = NSURL(string: URLArrayStringThisSeason[indexPath.row])
let placeholderImage = UIImage(named: "Rectangle")!
let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
size: imageView.frame.size,
radius: 0
)
imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
)
cell.layer.cornerRadius = 3.0
}
else if collectionView == whatsNewCollectionView {
let imageView = cell.viewWithTag(1) as! UIImageView
let url = NSURL(string: URLArrayStringRecents[indexPath.row])
let placeholderImage = UIImage(named: "Rectangle")!
let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
size: imageView.frame.size,
radius: 0
)
imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
)
cell.layer.cornerRadius = 3.0
}
return cell
}
在最后创建返回单元格,因为您只会点击一个语句,并且所有语句都需要返回一个单元格,因此不需要在三个不同的位置返回单元格,这足以将它放在最后。并且没有必要两次添加let cell = collectionView.dequeu...
,一次就足够了。
答案 1 :(得分:0)
并非上述代码中的所有控制路径都返回一个单元格。在'else if'body
之后添加另一个回报答案 2 :(得分:0)
您的代码归结为
function doSomething(a : Int) -> String {
if a == 1 {
return "something";
} else if a == 2 {
return "somethingElse";
}
}
编译器会抱怨,因为如果a
为3
会发生什么?该方法不会返回任何内容。 你可能知道a
永远不会是3,但编译器不知道。
因此,要么将else if ...
更改为简单的else
,要使函数始终返回OR,或者在else if之后返回一些内容。
答案 3 :(得分:0)
根据您在其他答案中发布的评论,您有三个(可能更多)集合视图,并希望拥有这样的代码结构:
array([[-3449.15531305, 4097.1707738 , 1142.52971904, 1566.51333847],
[ 4097.1707738 , -4863.70533241, -1356.66524959, -1860.15980716],
[ 1142.52971904, -1356.66524959, -378.32586814, -518.71965102],
[ 1566.51333847, -1860.15980716, -518.71965102, -711.21062988]])
您使用if collectionView == collectionView1 {
// code that returns some cell
} else if collectionView == collectionView2 {
// code that returns some other cell
} else if collectionView == collectionView3 {
// code that returns yet another cell
}
而不是else if
的原因是因为您希望稍后添加更多else
。
然后我想,调用else if
对你来说是最好的解决方案。
fatalError
这不仅会删除“没有所有代码路径返回值”错误,它还会提醒您是否忘记添加其他if!
此解决方案也可以应用于其他数据源方法!
答案 4 :(得分:0)
谢谢大家,接下来我会尝试切换方法但是现在我找到了一个非常好的方法:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let cellCount = 0
if collectionView == thisSeasonCollectionView {
let cellCount = URLArrayStringThisSeason.count
return cellCount
}else if collectionView == whatsNewCollectionView {
let cellCount = URLArrayStringRecents.count
return cellCount
}else if collectionView == labelCollectionView {
let cellCount = URLArrayStringLabel.count
return cellCount
}
return cellCount
}