简体   繁体   English

UICollectionView reloadData问题

[英]UICollectionView reloadData issue

I don't know what is happening, but my UICollectionView is not reloading from another class automatically. 我不知道发生了什么,但是我的UICollectionView没有自动从另一个类重新加载。

I have my UICollectionView in class1 and i am updating the data in it by calling a newArray from class2 我在class1中有我的UICollectionView,并且正在通过从class2调用newArray来更新其中的数据

    class1:

In view DidLoad

      UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    [aFlowLayout setItemSize:CGSizeMake(self.view.frame.size.width/2 - 15, self.view.frame.size.height/3 - 30)];
    [aFlowLayout setSectionInset:UIEdgeInsetsMake(10,10,10,10)];
    [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:aFlowLayout];

    [self.collectionView registerClass:[searchedPersonsCell class] forCellWithReuseIdentifier:@"SongCell"];
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
    self.collectionView.delegate = self;
    self.collectionView.dataSource=self;

    self.collectionView.scrollEnabled=YES;
    self.collectionView.alwaysBounceVertical=YES;
    [self.view addSubview:self.collectionView];

    [self.collectionView performSelector:@selector(reloadData) withObject:nil afterDelay:0];

-(void) reloadPersonsData:(NSMutableArray*)newArray
{
    NSLog(@"success");
    self.personsArray =newArray;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
        [self.collectionView performSelector:@selector(reloadData) withObject:nil afterDelay:0];
        [self.collectionView setNeedsDisplay];
        [self.collectionView setNeedsLayout];

        [CATransaction flush];
//        [NSTimer scheduledTimerWithTimeInterval:0.3 target:self.collectionView selector:@selector(reload) userInfo:nil repeats:NO];

        //tried all the above
    });
}

when user ask for more persons, i access class2 by a protocol, 当用户要求更多人时,我通过协议访问了class2,

Now In Class 2: 现在在课堂2:

I am Getting the new Array, passing it to the property array in class1(it is passed successfully and i checked from debug) then calling reloadPersonsData Function from class2 to class1(the nslog "success" is printed so it is accessing the method. But Nothing is happened!! numberOfItemsInSection or numberOfsection are not called!!and the uicollectionview is not updated 我正在获取新的数组,将其传递给class1中的属性数组(它已成功传递,并且我从调试中进行了检查),然后从class2到class1调用reloadPersonsData函数(打印了nslog“成功”,因此它正在访问该方法。)什么都没发生!numberOfItemsInSection或numberOfsection不被调用!并且uicollectionview不更新

The weird thing, if i called the same method reloadPersonsData inside class1 from a button , i got the UiCollectionView updated!!! 奇怪的是,如果我从一个按钮调用了class1内部的相同方法reloadPersonsData,我的UiCollectionView就会更新!!! so what is wrong here?why it is not reloaded when calling it from another class can anyone help me? 那么这是怎么回事?为什么从另一个类中调用它时却没有重新加载它,有人可以帮助我吗?

Thank you 谢谢

Just Check if the instance that class2 owned from class1 is the same as class1 只需检查class2拥有的class2实例是否与class1相同

Good Luck 祝好运

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

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