[英]UITableViewCell How can I detect that the user has swiped left or Right?
you have to add Gesture Recognizer in you cellForRowAtIndexPath 您必须在cellForRowAtIndexPath中添加Gesture Recognizer
UISwipeGestureRecognizer* swRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)];
[swRight setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:swRight];
UISwipeGestureRecognizer* swLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedLeft:)];
[swLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[cell addGestureRecognizer:swLeft];
and then its selector method 然后是它的选择器方法
-(void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// your code
}
}
-(void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// your code
}
}
Try this code it will works for you: 试试下面的代码,它将为您工作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:swipe];
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
return cell;
}
- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
//..
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.