在UITableView的 didSelectRowAtIndexPath中获取点击区域

项目中,要在UITableViewCell区分不同的点击区域,比如左边点击执行某个操作,右边点击执行另一个操作。原本我的方案是在cell的左边和右边各放一个透明的UIButton,点击两个button执行不同的操作,而controller中的didSelectRowAtIndexPath函数就设为空了。但是后来有个问题,就是可以同时用多个手指长按在不同的cell上,导致触发过个操作,而且cell的选中态也不好控制。

 

后来想到,UIview的触摸事件可以得到触摸的位置,那可不可以在cell的touch事件中得到位置,保存到一个变量中,然后didSelectRowAtIndexPath函数再根据这个变量执行不同的操作呢?

代码如下:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [[event allTouches] anyObject];
    CGPoint leftLocation = [touch locationInView: _bgViewLeft];
    CGPoint rightLocation = [touch locationInView: _bgViewRight];
    
    if ([_bgViewLeft pointInside:leftLocation withEvent:event])
    {
        self.touchLocation = eCellTouchLocationLeft;
        
        [super touchesBegan:touches withEvent:event];
    }
    else if ([_bgViewRight pointInside:rightLocation withEvent:event])
    {
        self.touchLocation = eCellTouchLocationRight;
        
        [super touchesBegan:touches withEvent:event];
    }
    else
    {
        self.touchLocation = eCellTouchLocationALL;
        
        [super touchesBegan: touches withEvent:event];
    }
}

 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UPAboutMeTableViewCell *cell = (UPAboutMeTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if (cell.touchLocation == eCellTouchLocationLeft || cell.touchLocation == eCellTouchLocationALL)
    {
        [self aboutMeTableViewCellonDidClickInLeft:cell];
    }
    else
    {
        [self aboutMeTableViewCellonDidClickInRight:cell];
    }
}

posted @ 2014-08-04 18:29  pinkong  阅读(3131)  评论(0编辑  收藏  举报