UITableView Edit mode

眉间皱痕 提交于 2019-12-18 10:44:50

问题


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:

- (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?


回答1:


as Anish pointed to using

[tableView setEditing: YES animated: YES]; 

But you need to have it in viewWillAppear view event to make it work.




回答2:


try this...

[tableView setEditing: YES animated: YES];



回答3:


In ViewDidLoad write

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(addORDoneRows)];
[self.navigationItem setLeftBarButtonItem:addButton];

addORDoneRow

- (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




回答4:


To load a tableView in edit mode you should call setEditing(true, animated: false) in 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).

Tested with Swift 2.2.




回答5:


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



来源:https://stackoverflow.com/questions/6001852/uitableview-edit-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!