6
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if(editingStyle == UITableViewCellEditingStyleDelete){
        //[theSimpsons removeObjectAtIndex:indexPath.row];

        NSString *time = [[arrayList objectAtIndex:indexPath.row] objectForKey:@"TIME"];
        NSString *date= [[arrayList objectAtIndex:indexPath.row] objectForKey:@"DATE"];
        DBManager *dbm = [[DBManager alloc] init];
        [dbm initiallzeDatabase];
        [dbm deleteItemAtIndexPath:time :date];

        //arrayList = [dbm getParkResults];
        //[parkTableView reloadData];

        [arrayList removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

我在我的应用程序中使用了上面的代码。编辑部分工作正常,但问题是,只有当我们在单元格上从左向右滑动时才会出现删除按钮。现在我有一个菜单,可以像 Facebook 一样从左到右滑动打开。那么我如何确保当用户从右向左滑动时出现删除按钮。

4

4 回答 4

6

请不要将代表设置为UISwipeGestureRecognizer

把这个写viewDidLoad在你发起你的UITableView

 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(callYourMethod:)];
 swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
 [self.tblView addGestureRecognizer:swipeRight];

不需要在下面的方法中写任何东西。

- (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer
{

}
于 2013-03-26T10:14:02.367 回答
2

这可能会奏效。将此代码放入awakeFromNibor init

UISwipeGestureRecognizer *swipeDelete = [[UISwipeGestureRecognizer alloc]     initWithTarget:self action:@selector(handleSwipeDelete:)];
[swipeDelete setDirection: UISwipeGestureRecognizerDirectionLeft];
[yourTableView addGestureRecognizer:swipeDelete];

然后在handleSwipeDelete组织您的单元格并显示删除按钮。您可能需要处理单元格状态并在 swipeLeft/swipeRight 上更改它。

于 2013-03-26T08:37:52.207 回答
1

看起来您需要自定义 UITableViewCell 的行为,我从未注意到 Apple 在从右向左滑动后提供了该行为....

于 2013-03-26T07:50:20.403 回答
1

你可以通过使用得到它two Methods of UITableVie.

// This only needs to be implemented if you are going to be returning NO
// for some items. By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
     //When you remove a cell of your tableview, you also has to remove your array object at index x
    }    
}

取出物体后。您必须重新加载UITableView.

[self.tableView reloadData];

欲了解更多信息,请阅读此官方文档。

于 2013-03-26T10:28:15.663 回答