0

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

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

2 回答 2

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];
}

didSelectRowAtIndexPath然后,您UITableViewController可以在其中访问wasTouchOnLeft单元格的变量。

于 2012-08-23T09:19:36.077 回答
1

您无法在此方法中直接确定这一点。但是如果你真的想要这个功能,可以创建一个自定义的 tableview 单元格,它有两个大按钮(一个在右侧,一个在左边),或者在单元格上应用一个触摸识别器。

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

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

但最好从表格单元格委派它而不是从 didSelectRow 访问它......</p>

于 2012-08-23T09:16:40.957 回答