问题描述
我刚将一个表插入到普通的UIViewController中,并将委托和源组件与文件的所有者连接起来。当我将数据插入表行时,一切正常。但现在我想知道如何删除行。
I just inserted a table into a normal UIViewController and connected the delegate and source components with the file's owner. Everything works fine when i insert data into the table rows. but now i am trying to find out how rows can be deleted.
我只看了很多其他帖子,但找不到合适的解决方案。
I just looked at a lot of other posts, but couldn't find the right solution.
我试图为表格中的每一行插入一个asseccory按钮:
I tried to insert a asseccory button for each row in the table:
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
我甚至找到了按下附件按钮时调用的方法:
i even found the method that will be called when the accessory button is pressed:
- (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];
}
在日志中打印消息,但我没有任何一种方法试图调用(评论者)确实将视图更改为编辑模式。我该如何解决这个问题呢?
Inside the log the message is printed, but no one of the methods that i tried to call (the commented one) did change the view to the edit mode. How can i solve this problem?
这是UIViewController的截图。我还没有集成一个navigationController。
Here is a Screenshot of the UIViewController. I haven't integrated a navigationController.
推荐答案
我可以解决删除行的问题。为了使它工作我插入像ACB告诉我,以下方法:
I could just solve the problem for deleting the rows. To make it work i inserted like ACB told me, the following methods:
//when blue accessory button has been pressed
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
//turn into edit mode
[tableView setEditing:YES animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
}
// method to enable the deleting of rows
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
Activity *activity = [allActivities objectAtIndex:indexPath.row];
// remove the item from the array
[allActivities removeObjectAtIndex:indexPath.row];
//delete element from database with created method
[dataController deleteFromTable:@"activity" theElementWithID:activity._id];
[tableView setEditing:NO animated:YES];
// refresh the table view
[tableView reloadData];
}
}
我在 cocoapi.wordpress.com/tag/caneditrowatindexpath
但我仍然只有一个问题。如果有人进入编辑模式,并且不会删除一行,则无法再次关闭编辑模式。如果您切换视图并再次返回它会自动停止,但否则不可能,因为我没有插入NavigationController。
But i still have just one problem. If somebody enters in editing mode, and won't delete a row, there is no possibility, to turn the editing mode off again. If you switch the view and come back again it autmatically stops, but otherwise its not possible because i don't have a NavigationController inserted.
有没有办法,可以访问编辑时行左侧的删除控件的状态?检测是否有人再次关闭删除以跳出编辑模式会很有用。
Is there a way, to access the state of the Deletion Control on the left of the row while editing? it would be useful to detect if somebody turned the deletion off again to jump out of editing mode.
它可能不适合苹果用户指南,但我刚刚在我的第一个项目,它应该工作。
It might not fit the apple user guide, but im just on my first project and it should just work.
这篇关于将Table更改为Edit模式并删除普通ViewController中的Rows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!