2016-08-12 59 views
0

當滾動到底部然後備份時,我的收藏視圖單元格正在修改。滾動時更改的快速收藏視圖數據

在我的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! 
} 
+0

您可以發佈您'FeedThumbnail'類? – RPK

+0

我剛添加它。 – Basic

+0

這些按鈕都在holder_view – Basic

回答

0

您在cellFor...有一個條件,如:

if(cell.picture.frame.origin.x == 0){... 

如果電池被重用,這"left"但不滿足你的條件,所以它會保持它的外觀與重用前相同。你應該這樣做。這裏是示例代碼:

if(cell.picture.frame.origin.x == 0){ 
    ... 
} else { 
    // make it a left cell 
} 
+0

我並不完全遵循:/。所以基本上我應該切換我的代碼,以在else語句而不是if語句中進行調整?這不會在每次細胞進入視野時修改內容對齊嗎? – Basic

+0

@TomGreco對不起,我表達不好。當你有一個重複使用的單元時,它可能會離開,向右或居中。你應該刷新它,否則你會得到一個原始外觀的單元格。你明白嗎? – Lumialxk

0

像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() 
} 

}