简体   繁体   English

UICollectionView reloadData无法正常工作

[英]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. 我面临一个奇怪的问题,当我从一个动作(按钮)执行UICollectionView的reloadData时,单元格无法正确显示,只有单元格的背景图像正常。

My "INVPropertyCell" is composed with a background image and 2 labels (title & price). 我的“ INVPropertyCell”由背景图片和2个标签(标题和价格)组成。 When I execute the reloadData the above labels from cells disappeared randomly. 当我执行reloadData时,上面的单元格标签随机消失了。

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 您尝试检查并分配collectionView..cellForItemAtIndexPath中是否为null的单元格

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 :) 我正在快速编码,所以上面的语法可能是错误的,但这是个主意:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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