Ios UITableViewCell如何检测用户向左或向右刷卡?
我正在使用,我想知道当用户向左或向右滑动时触发的动作。您必须在cellForRowAtIndexPath中添加手势识别器Ios UITableViewCell如何检测用户向左或向右刷卡?,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,我正在使用,我想知道当用户向左或向右滑动时触发的动作。您必须在cellForRowAtIndexPath中添加手势识别器 UISwipeGestureRecognizer* swRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipedRight:)]; [swRight setDirection:UISwipeGestureRecognizerDirectionRight]
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];
然后是它的选择器方法
-(void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// your code
}
}
-(void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// your code
}
}
您必须在CellForRowatineXpath中添加手势识别器
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];
然后是它的选择器方法
-(void)cellSwipedRight:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// your code
}
}
-(void)cellSwipedLeft:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// your code
}
}
请尝试以下代码,它将适用于您:
- (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];
//..
}
}
请尝试以下代码,它将适用于您:
- (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];
//..
}
}
选中此选项此方法仅识别
UIgestureRecognitizerStateEnded
State@ahmedlabib它有多个选项用于UIgestureRecognitizerState
如开始、取消、更改、结束等。您可以尝试以下操作-(void)swipeableTableViewCell:(SWTableViewCell*)单元格滚动状态:(SWCellState)状态;“SWTableviewCell”方法感谢@wolverine这对meCheck非常有效此方法仅识别UIgestureRecognitizerStateEnd
State@ahmedlabib它有多个选项可用于UIgestureRecognitizerState
,如开始、取消、更改、结束等。您可以尝试此-(void)swipeableTableViewCell:(SWTableViewCell*)单元格滚动状态:(SWCellState)状态;“SWTableViewCell”的方法谢谢@wolverine这对我对@kb920说的MEA非常有效-这种方法只识别UIgestureRecognitizerStateEnded
状态。不……正如我在你的问题中所评论的那样,它识别多个状态UIgestureRecognitizerStateStart
,UIgestureRecognitizerStateCancelled
,UIgestureRecognitizerStateChanged
,UIgestureRecognitizerStateEnded
etcIt成功了,谢谢,还为此库找到了另一个解决方案-(void)swipeableTableViewCell:(SWTableViewCell*)单元格滚动状态:(SWCellState)state;
正如我对@kb920所说的那样-这种方法只识别UIgestureRecognitizerStateEnded
状态。不……正如我在你的问题中所评论的那样,UIgestureRecognitizerStateStart
,UIgestureRecognitizerStateCancelled
,UIgestureRecognitizerStateChanged
,UIgestureRecognitizerStateeEnded
etcIt工作了,谢谢,还为此库找到了另一个解决方案-(void)swipeableTableViewCell:(SWTableViewCell*)单元格滚动状态:(SWCellState)状态;