ios UITableView 编辑模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6001852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView Edit mode
提问by user739509
I have UITableView
and I am trying to load it by default in edit mode. The problem is when I this line table.editing=TRUE;
my rows disappear, I implemented this methods:
我有UITableView
并且我正在尝试在编辑模式下默认加载它。问题是当我这一行table.editing=TRUE;
我的行消失时,我实现了这个方法:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
but with no luck. What should I do?
但没有运气。我该怎么办?
回答by Ahmad Kayyali
as Anishpointed to using
正如安尼什指出的那样使用
[tableView setEditing: YES animated: YES];
But you need to have it in viewWillAppear
view event to make it work.
但是您需要在viewWillAppear
视图事件中使用它才能使其工作。
回答by Anish
try this...
尝试这个...
[tableView setEditing: YES animated: YES];
回答by Hardik Thakkar
In ViewDidLoad write
在 ViewDidLoad 中写入
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(addORDoneRows)];
[self.navigationItem setLeftBarButtonItem:addButton];
addORDoneRow
添加ORDoneRow
- (void)addORDoneRows
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[_dbSongsTblView setEditing:NO animated:NO];
[_dbSongsTblView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[_dbSongsTblView setEditing:YES animated:YES];
[_dbSongsTblView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
For MultipleSelection of Row
对于多选行
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
Note: There are Three Editiing Style as below
注意:有以下三种编辑风格
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
Note: And for multiple Selection set Selection and Editing Style to Multiple from Attribute inspector
注意:对于多个选择集,从属性检查器将选择和编辑样式设置为多个
回答by Andrej
To load a tableView in edit mode you should call setEditing(true, animated: false)
in viewDidLoad()
.
要加载在编辑模式下的tableView你应该调用setEditing(true, animated: false)
的viewDidLoad()
。
If your view controller is a subclass of UITableViewController
there's no need to change, just make the above call. Otherwise if your view controller is a subclass of UIViewController, then you should make a call in this way: tableView.setEditing(true, animated: true)
.
如果您的视图控制器是UITableViewController
不需要更改的子类,只需进行上述调用。否则,如果您的视图控制器的UIViewController的子类,那么你应该在这样一个电话:tableView.setEditing(true, animated: true)
。
Tested with Swift 2.2.
使用 Swift 2.2 测试。
回答by George
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
[self.tableView setEditing:!self.tableView.isEditing 动画:YES];