我在工具栏中添加了一个栏按钮项,并在下面给出的方法中实现了它的功能。但是,我不能在每一行中显示一个删除按钮。我该怎么做呢?
-(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];
}}
发布于 2011-05-25 14:45:48
工具栏上的按钮需要连接到自定义表视图控制器子类中的接口操作。按如下方式定义操作-
- (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。
https://stackoverflow.com/questions/6120296
复制相似问题