1
我的UICollectionView
有一個奇怪的行爲,當用戶點擊UICollectionViewCell
。IOS UICollectionView選擇問題
在我的假設中,根據蘋果文檔,當用戶點擊UICollectionViewCell
時,單元格應該變亮,然後選中。
但在我的應用程序中,當用戶點擊單元格時,它只會變亮,而不會被選中。
而當用戶在單元格上滑動時,只有在這種情況下,單元格纔會被選中。
任何幫助,請。使用Xcode 6。
我使用UICollectionView從自定義UICollectionViewCell類,它覆蓋setSelected
和setHighlighted
。我已經實施了這些方法
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
但是僅用於檢查。
UPD:
我拍攝的視頻http://take.ms/LzBkZ。
另外提供的代碼:
**UICollectionViewController**
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"should");
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"highlighted");
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"select %@", indexPath);
_selectedCategory = _source[(NSUInteger) indexPath.row];
// _selectedNumber = [NSNumber numberWithInteger:category.id];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"deselect %@", indexPath);
if (_selectedCategory) {
_selectedCategory = nil;
}
}
而且
**SSCustomViewCell**
- (void)setSelected:(BOOL)selected
{
[super setSelected:selected];
self.alpha = (CGFloat) (selected ? 0.4 : 1);
[self setNeedsDisplay];
}
- (void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
self.alpha = (CGFloat) (highlighted ? 0.5 : 1);
[self setNeedsDisplay];
}
確保您調用'super'在重寫'的setSelected:'和'setHilighted:' ,並從'collectionView:shouldSelectItemAtIndexPath:' – rintaro 2014-09-22 11:22:05
返回'YES'當然,我這樣做。將代碼和視頻添加到問題 – wtorsi 2014-09-22 11:31:15
因此,您的問題是「collectionView:shouldSelectItemAtIndexPath:」甚至沒有被調用。你使用了什麼'UIGestureRecognizer'? – rintaro 2014-09-22 14:22:47