开发者

Dynamically add a new row as Add in UITableView in edit mode?

开发者 https://www.devze.com 2022-12-25 20:51 出处:网络
I have a table view with 0 or n rows of data from datastore. I added a customized Edit button on the right of the view\'s navigation bar. By default, when the edit button is clicked, in the action eve

I have a table view with 0 or n rows of data from datastore. I added a customized Edit button on the right of the view's navigation bar. By default, when the edit button is clicked, in the action event, I set the view as edit mode:

开发者_StackOverflow中文版[self.tableView setEditing:YES animated:YES];

I would like to add a row at the end of the table view with Add button as an accessory on the left when the table view is in edit mode. And the "Add" row will not be displayed when the view is not in edit mode. This is very similar to the case of iPhon'e Contacts application when a contact is in edit mode.

I am not sure if I need to add a row dynamically, and how if so? An alternative way I guess is to add more row when tableView:numberOfRowsInSection: is called? If later is the case, I have to make it hidden when the view is not in edit mode and visible when the view is in edit.

By the way, my table view is loaded from xib file. Not sure if there are any settings there to specify the table view's style as UITableViewCellEditingStyleDelete and UITableViewCellEditingStyInsert to enable the add feature?


You need to implement a bunch of delegate methods. On the UITableViewDelegate side:

- (UITableViewCellEditingStyle) tableView: (UITableView *) tableView 
editingStyleForRowAtIndexPath: (NSIndexPath *) indexPath

On the UITableViewDataSource side:

- (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle 
 forRowAtIndexPath: (NSIndexPath *) indexPath

You can just add new rows to your data source, and then call [tableView reloadData] or [tableView insertRowsAtIndexPaths:...].

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消