iphone - 如何添加 UITableView setEditing 提供的插入和删除按钮?

标签 iphone objective-c ios

我想为表格 View 中的各个单元格添加插入和删除按钮。

以下代码对于删除插入效果很好,我也希望按钮不在导航中。

- (void)viewDidLoad {
    [super viewDidLoad];

    card = [[NSMutableArray alloc]initWithObjects:@"card1",@"Card2",@"card3",@"card4",nil];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)];
    self.navigationItem.rightBarButtonItem = rightButton;


    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [card removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}

最佳答案

要删除导航栏中的编辑按钮,请删除此行:

self.navigationItem.leftBarButtonItem = self.editButtonItem;

然后创建您自己的按钮,并通过其操作自行调用 [tableview setEditing:]

要为特定单元格而不是其他单元格启用删除按钮,请实现 tableview 委托(delegate)方法:

tableView:editingStyleForRowAtIndexPath:

对于您不想在编辑模式下显示删除按钮的任何行,返回 UITableViewCellEditingStyleNone ;对于您确实想要显示删除按钮的行,返回 UITableViewCellEditingStyleDelete

关于iphone - 如何添加 UITableView setEditing 提供的插入和删除按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8861775/

相关文章:

ios - UIScrollView 中的 UITableView

iphone - UITextFields 的 NSArray,我怎样才能找到一个特定的?

ios - 找不到 AmazonServiceException Unmarshaller.h 文件

ios - 如何实现具有多个单元格的 UICollectionVIew

ios - 停止调用 -removeAllAnimaitions

ios - UIViewController 已正确释放,但其 UIView 未正确释放?

iphone - 在 MonoTouch 中调用 REST 服务的首选方式是什么?

iphone - App Store 提交错误 : A symbolic link resolves to a location outside of the binary

ios - 在 Xcode 中创建动态预处理器宏

ios - 使用 NSPredicate 比较两个数组并返回重复的字符