gpt4 book ai didi

ios - 将表更改为编辑模式并删除普通 ViewController 中的行

转载 作者:可可西里 更新时间:2023-11-01 05:04:43 27 4
gpt4 key购买 nike

我只是将一个表插入到一个普通的 UIViewController 中,并将委托(delegate)和源组件与文件的所有者连接起来。当我将数据插入表格行时,一切正常。但现在我正试图找出如何删除行。

我只是看了很多其他帖子,但找不到正确的解决方案。

我试图为表中的每一行插入一个辅助按钮:

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

我什至找到了按下辅助按钮时将调用的方法:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

NSLog(@"Accessory pressed");

//[self tableView:tableView willBeginEditingRowAtIndexPath:indexPath];

//[self tableView:nil canEditRowAtIndexPath:indexPath];
//[self setEditing:YES animated:YES];
}

在日志中打印了消息,但我尝试调用的任何一种方法(注释的方法)都没有将 View 更改为编辑模式。我该如何解决这个问题?


这是 UIViewController 的屏幕截图。我没有集成导航 Controller 。

enter image description here

最佳答案

为了启用表格 View 的编辑模式,您可以调用 UITableView 上的编辑方法,

[self.tableView setEditing:YES animated:YES];

您必须实现 tableView:commitEditingStyle:forRowAtIndexPath: 方法才能删除行。要删除行,您需要使用 deleteRowsAtIndexPaths:withRowAnimation: 方法。

例如:-

self.navigationItem.rightBarButtonItem = self.editButtonItem;//set in viewDidLoad

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { //Implement this method
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //implement the delegate method

if (editingStyle == UITableViewCellEditingStyleDelete) {
// Update data source array here, something like [array removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

有关更多详细信息,请查看 apple documentation here .

关于ios - 将表更改为编辑模式并删除普通 ViewController 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14148229/

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