将表更改为编辑模式并删除普通 ViewController 中的行 [英] change Table to Edit mode and delete Rows inside a normal ViewController

查看:22
本文介绍了将表更改为编辑模式并删除普通 ViewController 中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是将一个表插入到一个普通的 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.

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

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 的屏幕截图.我还没有集成导航控制器.

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.

这篇关于将表更改为编辑模式并删除普通 ViewController 中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆