2014-04-12 72 views
0

基本上,我有一個從數據源加載值的UICollectionViewCell,另外它應該設置子視圖幀的高度等於數據源中的一個值。爲什麼我的單元格子視圖在滾動時會發生變化UICollectionView

我遇到的問題是它在第一次運行項目時看起來正確,但當我多次來回滾動時,由於單元格正在被重用,文本值保持不變,但子視圖的框架變化。

令我困惑的是,設置單元格中標籤中文本的變量與設置同一單元格中子視圖的高度值的變量相同;但標籤文本總是正確的,但UIView幀的高度隨着滾動而不斷變化。

我知道這可能與細胞如何被重用有關,但我不能把它放在手指上。

下面是我對cellForRowAtIndexPath的代碼。謝謝!

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
{ 

DailyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DayCell" forIndexPath:indexPath]; 

cell.backgroundContainerView.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.3f]; 

float total = [[dummyData objectAtIndex:indexPath.row][@"total"] floatValue] * 2; 

UIView * backgroundFillView = [UIView new]; 
if (![cell viewWithTag:1000]) { 
    NSLog(@"Creating backgroundFillView on Cell: %ld", (long)indexPath.row); 

    backgroundFillView.tag = 1000; 
    [cell.backgroundContainerView addSubview:backgroundFillView]; 

} 


cell.debugCellNumber.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
cell.debugCellTotal.text = [NSString stringWithFormat:@"%f", total]; 

backgroundFillView.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f]; 
backgroundFillView.frame = CGRectMake(0, 200 - total, 60, total); 

NSLog(@"Cell: %ld, total: %f", (long)indexPath.row, total); 
NSLog(@"Cell: %ld, backgroundFillView Height: %f", indexPath.row, backgroundFillView.frame.size.height); 
NSLog(@"Cell: %ld, backgroundFillView Y: %f", indexPath.row, backgroundFillView.frame.origin.y); 

return cell; 
} 

回答

1

您在第一次填充單元格時只添加一個backgroundFillView。不重新使用時。

替換:

UIView * backgroundFillView = [UIView new]; 
if (![cell viewWithTag:1000]) { 
    NSLog(@"Creating backgroundFillView on Cell: %ld", (long)indexPath.row); 

    backgroundFillView.tag = 1000; 
    [cell.backgroundContainerView addSubview:backgroundFillView]; 

} 

有:

UIView * backgroundFillView = [cell viewWithTag:1000]; 
if (! backgroundFillView) { 
    NSLog(@"Creating backgroundFillView on Cell: %ld", (long)indexPath.row); 
    backgroundFillView = [UIView new]; 

    backgroundFillView.tag = 1000; 
    [cell.backgroundContainerView addSubview:backgroundFillView]; 

} 
+0

哦~~是的,我看到的。就是這樣,現在好,非常感謝! – user3525727

相關問題