想要在UITableView中从右向左滑动显示删除按钮

时间:2013-03-26 07:29:17

标签: iphone objective-c uitableview

- (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 个答案:

答案 0 :(得分:6)

请不要将代表设置为UISwipeGestureRecognizer

viewDidLoad或您发起UITableView

的位置写下此内容
 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(callYourMethod:)];
 swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
 [self.tblView addGestureRecognizer:swipeRight];

无需在下面的方法中写任何内容。

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

}

答案 1 :(得分:2)

这可能有用。将此代码放在awakeFromNibinit

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

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

答案 2 :(得分: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];

了解更多信息Read This OfficialDocumentaion.

答案 3 :(得分:0)

看起来你需要自定义UITableViewCell的行为,我从来没有注意到Apple在从右向左滑动后提供了这种行为....

相关问题