在UITableIview中向后滑动表格视图单元格时设置操作

时间:2016-11-28 10:40:09

标签: ios objective-c uitableview

我正在使用tableview列出文件。而且效果很好。但是,当我向左滑动删除时,tableview进入编辑模式,我的编辑按钮被取消按钮覆盖。这也很完美。但是当我向后滑动而没有删除时,按钮应该返回编辑,这不会发生。当一个tableview单元被刷回时是否有被调用的委托?这是相关的代码

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[[NSUserDefaults standardUserDefaults]objectForKey:DOCUMENTDELETE]isEqualToString:ENABLED]) {

return YES;
} else {
    return NO;
}
}

-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView setEditing:NO];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
       editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.tableView.editing) {
    return UITableViewCellEditingStyleDelete;
}
else {
    self.navigationItem.rightBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = self.cancelButton;
    return UITableViewCellEditingStyleDelete;
}
}

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

self.navigationItem.rightBarButtonItem = nil ;
self.navigationItem.rightBarButtonItem = self.editButton;
if (editingStyle == UITableViewCellEditingStyleDelete) {
    NSString *cellText;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    cellText = selectedCell.textLabel.text;
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName =[NSString stringWithFormat:@"/Inbox/"];
    NSString *allFilesPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSString *theactualFilePath = [allFilesPath stringByAppendingPathComponent:cellText];

    NSError *error;

    _success = [[NSFileManager defaultManager] removeItemAtPath:theactualFilePath error:&error];
     [self.tableView setEditing:NO];
    if ([_filePathsArray count] == 0) {
        [[NSUserDefaults standardUserDefaults] setObject:DISABLED forKey:NOMATCHFOUND];
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:ALLFILESDELETED];
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    if (_success) {
    }
    else
    {
        NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
    }
 }

[self.tableView reloadData];

}

这是ViewDidLoad中的代码:

if ([[[NSUserDefaults standardUserDefaults] objectForKey:DOCUMENTDELETE] isEqualToString:ENABLED]) {
    if (self.tableView.editing){
        self.navigationItem.rightBarButtonItem = nil;
        self.navigationItem.rightBarButtonItem = self.cancelButton;
    }else {
    self.navigationItem.rightBarButtonItem = nil;
[self.navigationItem setRightBarButtonItem:self.editButton];
    }} else {
        self.navigationItem.rightBarButtonItem = nil;
    }

提前致谢

1 个答案:

答案 0 :(得分:1)

听起来你只需要实现委托方法

tableView:didEndEditingRowAtIndexPath:

您可以在此处捕获从编辑状态取消设置的表格视图单元格。