简体   繁体   中英

When calling -reloadData, -collectionView:didSelectItemAtIndexPath: not called

When calling -reloadData several times, -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath: not called.
(When calling once or twice, the method is called correctly.)
Running -reloadData is not taking a lot of time. Memory or CPU usage is also not busy.
Why are there times when -collectionView:didSelectItemAtIndexPath: is not called?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{ 
    CustomCollectionCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"select %d", indexPath.item);
    [collectionView reloadData];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"deselect %d", indexPath.item);
    [collectionView reloadData];
}

CustomCollectionCell.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {        
        //
    }
    return self;
}

Also you have to add numberOfItemsInSection function to your code. It returns the number of items in the specified section. Without this you can't call collectionView:didSelectItemAtIndexPath function.

Also call your [collectionView reloadData]; in your viewDidLoad or viewDidAppear.

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.yourArray count];
}

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