我做的第一件事是设置选定的单元格.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.selected = YES;
return cell;
}
Run Code Online (Sandbox Code Playgroud)
并且成功选择了单元格.如果用户触摸所选单元格,则应取消选择单元格并调用委托.但这绝不会发生.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
}
Run Code Online (Sandbox Code Playgroud)
我知道如果我以编程方式设置选择,则不会调用委托.委托和数据源已设置.
但是,此委托被调用:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __PRETTY_FUNCTION__);
return YES;
}
Run Code Online (Sandbox Code Playgroud)
如果我删除了cell.selected = YES一切正在工作.有没有人可以解释这种行为?