繁体   English   中英

检测已单击uitableviewcell的哪一侧:左或右

[英]Detect which side of uitableviewcell has been click: left or right

是否可以区分uitableview中行的哪一侧被单击?

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

您可以继承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];
}

然后,您可以在UITableViewControllerdidSelectRowAtIndexPath中访问该单元格的wasTouchOnLeft变量。

您无法通过此方法直接确定。 但是,如果您真的想要此功能,请创建一个自定义的Tableview单元格,该单元格具有两个大按钮(一个在左侧,一个在右侧),或者在该单元格上应用触摸识别器。

如果您将此操作委托给您的控制器(您也可以将其存储在单元格中,并通过进行访问)

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(((YourCustomTableCell*)[tableView cellForRowAtIndexPath:indexPath]).clickedSideProperty) action;
else anotherAction;
}

但是最好是从表单元格中委派,而不是从didSelectRow中访问它…

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM