gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:50:56 24 4
gpt4 key购买 nike

我想为表格 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com