Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Edit mode

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?

like image 390
user739509 Avatar asked May 14 '11 12:05

user739509


People also ask

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.

What does Tableview delegate do?

Configuring rows for the table view. Tells the delegate the table view is about to draw a cell for a particular row. Asks the delegate to return the level of indentation for a row in a given section. Called to let you fine tune the spring-loading behavior of the rows in a table.


3 Answers

as Anish pointed to using

[tableView setEditing: YES animated: YES]; 

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

like image 177
Ahmad Kayyali Avatar answered Oct 23 '22 08:10

Ahmad Kayyali


try this...

[tableView setEditing: YES animated: YES];
like image 10
Anish Avatar answered Oct 23 '22 09:10

Anish


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

like image 7
Hardik Thakkar Avatar answered Oct 23 '22 10:10

Hardik Thakkar