调用-reloadData时,-collectionView:didSelectItemAtIndexPath:未调用

时间:2015-05-13 11:16:15

标签: ios objective-c uicollectionview

多次致电-reloadData时,-collectionView:didSelectItemAtIndexPath:-collectionView:didDeselectItemAtIndexPath:未被致电 (当调用一次或两次时,正确调用该方法。)
运行-reloadData并不需要花费很多时间。内存或CPU使用率也不旺 为什么没有调用-collectionView:didSelectItemAtIndexPath:

- (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;
}

1 个答案:

答案 0 :(得分:0)

此外,您还必须在代码中添加 numberOfItemsInSection 功能。它返回指定部分中的项目数。如果没有此功能,您无法调用 collectionView:didSelectItemAtIndexPath 功能。

还可以在viewDidLoad或viewDidAppear中调用[collectionView reloadData];

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