如何在表格单元格上滑动并使其调用函数?

Ale*_*lex 4 iphone cocoa-touch ios

如何在表格单元格上滑动手指并调用函数(我将视图控制器推送到导航控制器)?

Mor*_*ast 8

只需UISwipeGestureRecognizer在单元格中添加一个.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    UISwipeGestureRecognizer *g = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellWasSwiped:)];
    [cell addGestureRecognizer:g];
    [g release];
}
Run Code Online (Sandbox Code Playgroud)

然后实现处理滑动的方法:

- (void)cellWasSwiped:(UIGestureRecognizer *)g {
    NSLog(@"Swiped");
}
Run Code Online (Sandbox Code Playgroud)

  • 您可能还想添加:`CGPoint p = [gestureRecognizer locationInView:TableView]; NSIndexPath*indexPath = [TableView indexPathForRowAtPoint:p]; if(indexPath == nil){//不在单元格上} else {//在单元格上,使用indexPath做某事.}` (4认同)