简体   繁体   English

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

[英]Want to present the delete button on right-to-left swipe in 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];
    }
}

I have used the above code in my app. 我在我的应用程序中使用了上面的代码。 The editing part works fine, but the thing is, the delete button appears only when we swipe from left-to-right on a cell. 编辑部分工作正常,但事实是,删除按钮仅在我们从单元格从左向右滑动时出现。 Now i have a menu that opens on left-to-right swipe like Facebook. 现在我有一个菜单,可以像Facebook一样从左到右滑动。 So how can i make sure that the delete button appears when the user swipes from right-to-left. 那么当用户从右向左滑动时,如何确保显示删除按钮。

Please dont set the delegate to UISwipeGestureRecognizer 请不要将UISwipeGestureRecognizer设置为UISwipeGestureRecognizer

Write this in viewDidLoad or where you have initiated your UITableView viewDidLoad或你发起UITableView地方写这个

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

No need to write anything in the below method. 无需在下面的方法中写任何东西。

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

}

This might work. 这可能会奏效。 Put this code in awakeFromNib or init : 将此代码放在awakeFromNibinit

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

then in handleSwipeDelete organize your cell and show delete button. 然后在handleSwipeDelete组织你的单元格并显示删除按钮。 You will probably need to handle cell state and change it on swipeLeft/swipeRight. 您可能需要处理单元状态并在swipeLeft / swipeRight上更改它。

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

You can got it by using two Methods of UITableVie. 您可以使用two Methods of UITableVie.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
    }    
}

After removing the object. 删除对象后。 You have to reload the UITableView . 你必须重新加载UITableView

[self.tableView reloadData];

For More Information Read This OfficialDocumentaion. 欲了解更多信息,请阅读此官方文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM