2014-03-03 152 views
0

後,我有一個基本的UICollectionView,如果我滾動將「重繪」對細胞的頂部的標籤,IOS UICollectionView細胞不刷新滾動

enter image description here

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


    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 


    cell.backgroundColor=[UIColor greenColor]; 

    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)]; 
    [cell.contentView addSubview:title]; 

    //NSString *titleLbl = [NSString stringWithFormat:@"i = %d", indexPath.row]; 


    title.text = [self.arraya objectAtIndex:indexPath.row]; 

    return cell; 
} 

如何解決它,所以它刷新滾動後的正確單元格? 乾杯

+0

見http://stackoverflow.com/questions/16517844/uicollectionview-reload-data-issue,HTTP:// stackoverflow.com/questions/21753707/uilabel-overloading-in-uicollectionview,http://stackoverflow.com/questions/21798907/preventing-uicollectionview-from-redrawing等 – Anna

回答

2

當你這樣做:

UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)]; 
[cell.contentView addSubview:title]; 

你每次創建一個新的標題,並將其添加到您的內容查看。相反,嘗試加入代碼,您的UILabel,並更改現有的UILabel的文本,像這樣:

UILabel *title = (UILabel *)[cell.contentView viewWithTag:SOME_NUMBER]; 
if (!title) { 
    title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)]; 
    title.tag = SOME_NUMBER; 
    [cell.contentView addSubview:title]; 
} 
title.text = @"your new text"