简体   繁体   中英

UICollectionView reloadData doesn't work correctly

I'm face a strange problem, when I execute from an action (button) the reloadData of my UICollectionView the cells are not displayed correctly, only the background image of the cells are ok.

My "INVPropertyCell" is composed with a background image and 2 labels (title & price). When I execute the reloadData the above labels from cells disappeared randomly.

I have done a lot of searches in the different forums, some people have the same problem but I didn't find out a fix.

Below, you will find my code, if someone can help me it would be very very appreciated.

Jérôme.

- (void)viewDidLoad
{
    NSLog (@"INVPropertiesViewController -- viewDidLoad");


    [super viewDidLoad];

    // Enregistrement de la cellule.
    [self.clProperties registerNib:[UINib nibWithNibName:@"INVPropertyCell" bundle:nil] forCellWithReuseIdentifier:@"propertyCell"];

    // Enregistrement du header de section
    [self.clProperties registerNib:[UINib nibWithNibName:@"INVPropertyHeaderSection" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCollectionCell" ];


    self.clProperties.delegate = self;
    self.clProperties.dataSource =self;


    NSLog (@"INVPropertiesViewController -- End of viewDidLoad");
}

- (void)viewWillAppear:(BOOL)animated
{
    NSLog (@"INVPropertiesViewController -- viewWillAppear");

    [super viewWillAppear:animated];

    categoryDictionary = [ServiceDatas getCategoriesDictionary];
    tblProperties = [(NSArray*)[ServiceDatas getListPropertiesByLieuFromLocalDataStore:self.selectedResidence] mutableCopy];

    if(tblProperties.count==0 & self.selectedResidence.propertiesCount.longValue >0)
        tblProperties = [(NSArray*)[ServiceDatas getListPropertiesByLieuFromServer:self.selectedResidence] mutableCopy];

    tblPropertiesByCategory = [ServiceDatas createCategoryBreakDown:tblProperties];

    keysCategories = [tblPropertiesByCategory allKeys];


    [self.clProperties reloadData];


    NSLog (@"INVPropertiesViewController -- End of viewWillAppear");
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
   // [self.clProperties.collectionViewLayout invalidateLayout];
    return tblPropertiesByCategory.count;
}

- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    UICollectionReusableView *reusableview = nil;

    if(kind == UICollectionElementKindSectionHeader){


        INVPropertyHeaderSection *headerSection = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCollectionCell" forIndexPath:indexPath];

        id categoryId = [keysCategories objectAtIndex:indexPath.section];

        CategoryModel *category = categoryDictionary[categoryId];

        if(category!=nil){

            headerSection.backgroundColor = [Utils colorFromHexString:category.color];
            headerSection.lblCategory.text = [category.title uppercaseString];

        }


        reusableview = headerSection;


    }else if(kind == UICollectionElementKindSectionFooter){

        if (reusableview==nil) {
            reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        }

    }

    return reusableview;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    id categoryId = [keysCategories objectAtIndex:section];
    NSMutableArray *tblProperties = [tblPropertiesByCategory objectForKey:categoryId];


    return [tblProperties count];

}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    INVPropertyCell *cell = nil;

    id categoryId = [keysCategories objectAtIndex:indexPath.section];

    NSMutableArray *tblProperties = [tblPropertiesByCategory objectForKey:categoryId];


    if(tblProperties!=nil){

        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"propertyCell" forIndexPath:indexPath];

        PropertyModel *property = [tblProperties objectAtIndex:indexPath.item];

        NSLog(@"Ligne %ld - Colonne %ld - Lieux %@",indexPath.section, (long)indexPath.item, property.title );

        CategoryModel *category = categoryDictionary[categoryId];

        if(category!=nil){

            [cell initWithProperty:property backgroundColor:[Utils colorFromHexString:category.color]];

        }


    }


    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    // Récupération du bien courant.
    id categoryId = [keysCategories objectAtIndex:indexPath.section];

    NSMutableArray *tblProperties = [tblPropertiesByCategory objectForKey:categoryId];

    PropertyModel *property = [tblProperties objectAtIndex:indexPath.row];

    NSLog(@"Property sélectionnée : %@",property.title);

    self.selectedProperty = property;

    if(property!=nil && [property.title isEqualToString:EMPTY_PROPERTY]){
        self.selectedProperty = nil;
    }

    // Déclenche le Segue pour aller à l'écran "Property"
    [self performSegueWithIdentifier:@"segueEditPropertyView" sender:self];

}

// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGSize mElementSize = CGSizeMake(107, 106);

    if(indexPath.item==0){
        mElementSize = CGSizeMake(106, 106);
    }

    return mElementSize;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0;
}

// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}

- (IBAction)btnRefresh:(id)sender {

    [self.clProperties reloadData];

}

我认为,您必须先为INVPropertyCell和INVPropertyHeaderSection单元注册笔尖,然后再注册类。

You try check and alloc cell if it is null in collectionView..cellForItemAtIndexPath

cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"propertyCell" forIndexPath:indexPath];
if (cell == null) {
    Array *xibs = [[NSBundle mainBundle] loadNibWithNamed:@"propertycell" 
...];
cell = xibs[0];
}

I'm coding in swift, so maybe above syntax is wrong, but this is idea :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM