【发布时间】:2013-12-04 16:21:12
【问题描述】:
我要做的第一件事是设置选中的单元格。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.selected = YES;
return cell;
}
并且单元格被成功选中。 如果用户触摸选定的单元格,则应取消选择该单元格并调用代表。但这永远不会发生。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
我知道如果我以编程方式设置选择,则不会调用代表。 委托和数据源已设置。
但是,这个委托被调用:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
return YES;
}
如果我删除 cell.selected = YES,那么一切正常。有没有人可以解释这种行为?
【问题讨论】:
-
当您使用 cellforItemAtIndexPath 时,它会为 tableview 中的每个单元格调用,所以基本上您正在尝试设置每个选定的单元格。你想达到什么目的?
标签: ios objective-c uicollectionview