滚动时更改的快速收藏视图数据
问题描述:
当滚动到底部然后备份时,我的收藏视图单元格正在修改。滚动时更改的快速收藏视图数据
在我的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!
}
答
您在cellFor...
有一个条件,如:
if(cell.picture.frame.origin.x == 0){...
如果电池被重用,这"left"
但不满足你的条件,所以它会保持它的外观与重用前相同。你应该这样做。这里是示例代码:
if(cell.picture.frame.origin.x == 0){
...
} else {
// make it a left cell
}
答
像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()
}
}
您可以发布您'FeedThumbnail'类? – RPK
我刚添加它。 – Basic
这些按钮都在holder_view – Basic