检测uitableviewcell的哪一侧已被点击:向左或向右

use*_*723 0 iphone xcode objective-c

是否可以区分uitableview中哪一行被点击?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  //which side of row has been detected left or right
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*ral 7

您可以继承UITableViewCell.在您的子类中,您可以创建一个iVar wasTouchOnLeft.然后覆盖touchesBegantouchesEnded类似下面的示例:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=(UITouch *)[touches anyObject];
    if([touch locationInView:self].x< (0.5 * self.frame.size.width) )
    {
        NSLog(@"Touched Left");
        wasTouchOnLeft=YES;
    }
    else {
        NSLog(@"Touched Right");
        wasTouchOnLeft=NO;
    }
    [super touchesEnded:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)

didSelectRowAtIndexPath你的UITableViewController你就可以访问该wasTouchOnLeft变量的单元格.