使UICollectionViewCell的一部分不可点击

时间:2014-03-31 22:14:36

标签: ios ios7 ios6 uicollectionviewcell

基本上我想要做的是在UICollectionView中有一个单元格,它的一部分将响应tap,而其中一部分不会被激活。 此单元格将是一个正常的单元格,在点击时会展开,在展开的区域上显示一个不响应点击的子视图。

我考虑过扩展单元格并在其上方添加 的子视图,这是不可点击的。但我认为这是一种“不好的”#34;完成它的方式。

有最好的方法吗?

这就是我想要做的事情:

enter image description here

感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

您需要子类UICollectionViewCell,然后覆盖hitTest:withEvent而不是pointInside:withEvent,在hitTest:withEvent:内您应该定义targetRect,然后检查点用户是否点击了在targetRect内。

以下代码适用于我的tableViewCell(类似于collectionViewCell)。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    CGRect targetRect = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 20);
    if (CGRectContainsPoint(targetRect, point)) {
        return nil;
    } else {
        return [super hitTest:point withEvent:event];
    }
}

答案 1 :(得分:0)

使用选择器

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

你必须返回一个单元格,在这个函数中你应该确定单元格是否可点击,然后再单击

cell.userInteractionEnabled = shouldAllowTouch;