2015-05-13 2 views
1

При вызове -reloadData несколько раз, -collectionView:didSelectItemAtIndexPath: или -collectionView:didDeselectItemAtIndexPath: не вызывается.
(При вызове один или два раза метод вызывается правильно.)
Запуск -reloadData не занимает много времени. Использование памяти или процессора также не занято.
Почему бывают случаи, когда -collectionView:didSelectItemAtIndexPath: не называется?При вызове -reloadData, -collectionView: didSelectItemAtIndexPath: 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; 
} 
+0

Зачем было '-collectionView: didSelectItemAtIndexPath:' вызываться, когда вы '-reloadData'? – Schemetrical

+0

из-за изменения свойства ячейки и отражения изменения в ячейке путем вызова '-reloadData'. –

+0

Да, но '-collectionView: didSelectItemAtIndexPath:' вызывается, когда пользователь удаляет ячейку. – Schemetrical

ответ

0

Также вы должны добавить numberOfItemsInSection функцию кода. Он возвращает количество элементов в указанном разделе. Без этого вы не можете позвонить collectionView: didSelectItemAtIndexPath функция.

Также позвоните в ваш [collectionView reloadData]; в вашем представленииDidLoad или viewDidAppear.

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

Спасибо. Но записывается метод '-collectionView: numberOfItemsInSection:'. –

+0

Вы проверили данные и данные делегата collectionView? –

Смежные вопросы