我的集合视图单元格在滚动到底部然后返回时被修改。
在我的viewDidLoad中,我有一个解析查询,它从数据库获取所有值,然后从主线程调用reloadData。
在初始加载时,可查看的单元格正确显示。
但是当我向下滚动时,加载了不可见的单元格,其中1/3显示不正确。
然后,当我向上滚动到初始的可视单元格时,第一个单元格不能正确显示。
不能正确显示的意思是,我的数据库中有一个用于对齐的字段。这可以包含居中、左或右字符串。
这个值是我的图像和按钮在单元格中的对齐方式。
这是我的cellForRow函数。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell: FeedThumbnail = collectionView.dequeueReusableCellWithReuseIdentifier("feed_cell", forIndexPath: indexPath) as! FeedThumbnail
//handle cell alignment
if(align[indexPath.row] == "left"){
//leave alignment as is for buttons
if(cell.picture.frame.origin.x == 0){
cell.picture.frame.offsetInPlace(dx: -75, dy: 0)
}
}
else if(align[indexPath.row] == "center"){
cell.holder_view.frame.offsetInPlace(dx: 105 - cell.holder_view.frame.origin.x, dy: 0)
//leave image as is
}
else if(align[indexPath.row] == "right"){
cell.holder_view.frame.offsetInPlace(dx: 220 - cell.holder_view.frame.origin.x, dy: 0)
cell.picture.frame.offsetInPlace(dx: 100 - cell.picture.frame.origin.x, dy: 0)
}
return cell
}
下面是我的集合视图单元格的类
import UIKit
class FeedThumbnail: UICollectionViewCell {
@IBOutlet weak var picture: UIImageView!
@IBOutlet weak var comment_btn: UIButton!
@IBOutlet weak var heart_btn: UIButton!
@IBOutlet weak var rescip_btn: UIButton!
@IBOutlet weak var og_btn: UIButton!
@IBOutlet weak var name_lbl: UIButton!
@IBOutlet weak var holder_view: UIView!
}
发布于 2016-08-12 10:38:59
就像Lumialxk和RPK说的那样。在修改单元格origin.x之前,我需要刷新它。
我想我不明白细胞会以这样的方式被重用。
解决方案是覆盖prepareForReuse,并在修改它之前将frame.origin.x设置回原始值。
导入UIKit
类FeedThumbnail: UICollectionViewCell {
@IBOutlet weak var picture: UIImageView!
@IBOutlet weak var comment_btn: UIButton!
@IBOutlet weak var heart_btn: UIButton!
@IBOutlet weak var rescip_btn: UIButton!
@IBOutlet weak var og_btn: UIButton!
@IBOutlet weak var name_lbl: UIButton!
@IBOutlet weak var holder_view: UIView!
override func prepareForReuse() {
picture.frame.origin.x = 0
holder_view.frame.origin.x = 0
super.prepareForReuse()
}
}
发布于 2016-08-12 10:22:25
在cellFor...
中有一个条件,如下所示:
if(cell.picture.frame.origin.x == 0){...
如果一个单元格被重用,它是"left"
,但不满足您的条件,因此它将保持与重用前相同的外观。你应该用这个做点什么。以下是示例代码:
if(cell.picture.frame.origin.x == 0){
...
} else {
// make it a left cell
}
https://stackoverflow.com/questions/38908588
复制相似问题