首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过UIBarButtonItem处理setEditing方法

无法通过UIBarButtonItem处理setEditing方法
EN

Stack Overflow用户
提问于 2011-05-25 14:16:34
回答 1查看 518关注 0票数 0

我在工具栏中添加了一个栏按钮项,并在下面给出的方法中实现了它的功能。但是,我不能在每一行中显示一个删除按钮。我该怎么做呢?

代码语言:javascript
复制
-(void)setEditing:(BOOL)editing
         animated:(BOOL)animated
{
    [super setEditing:editing
             animated:animated];
    [editButton setEnabled:!editing];
}

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
    NSString *selectedFile =  (NSString*) [directoryContents objectAtIndex: indexPath.row];
    NSString *selectedPath = [directoryPath stringByAppendingPathComponent:selectedFile];
    BOOL canWrite = [[NSFileManager defaultManager] isWritableFileAtPath:selectedPath];
    if(!canWrite)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"status"
                                message:@"path isnt writable" 
                                delegate:nil 
                                    cancelButtonTitle:@"cancel" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

    NSError *err = nil;
    if(! [[NSFileManager defaultManager] removeItemAtPath:selectedPath error:&err])
    {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"status"
                                                        message:@"cannot delete"
                                                       delegate:nil 
                                              cancelButtonTitle:@"cancel" 
                                              otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];

    }
    // Delete the row from the data source.
    NSArray *deletedPaths = [NSArray arrayWithObject: indexPath];
    [self loadDirectoryContents];
    [self.tableview deleteRowsAtIndexPaths:deletedPaths withRowAnimation:YES];
    }

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-25 14:45:48

工具栏上的按钮需要连接到自定义表视图控制器子类中的接口操作。按如下方式定义操作-

代码语言:javascript
复制
- (IBAction)startEditing {
    [self setEditing:YES animated:YES];
}

-(void)setEditing:(BOOL)editing
         animated:(BOOL)animated
{
    [super setEditing:editing
             animated:animated];
    [editButton setEnabled:!editing];
}

您必须实现委托方法tableView:editingStyleForRowAtIndexPath,并为希望启用它的行返回UITableViewCellEditingStyleDelete

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6120296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档