Neil的备忘录

just do it
posts - 66, comments - 8, trackbacks - 0, articles - 0

Iphone开发 可编辑表格

Posted on 2010-10-04 15:53 Neil's NoteBook 阅读(506) 评论(0)  编辑  收藏 所属分类: Iphone Development

In some of the iPhone's default apps, you may find that you can "edit" a table, then have little red minus signs appear to the left of the table, giving you the option to delete those rows. So, how do you do that in your own programs?

The answers, it turns out, is a very simple one:

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

That's literally all you need to do to set up those deletion marks. Then you just need to respond to tableView:commitEditingStyle: forRowAtIndexPath:.

However, there are some nuances, particularly the questions of how you start up a table's editing and how you end it, and I'm going to show some real-world examples of those methods over the course of this article ...

An Actual Example

The program that I've showed off elsewhere in this series makes uses of a tableView's editing functionality by allowing the user to delete characters from the app, each of whom are represented by a table row

First, you need to set up some way to activate the functionality:

- (void)editCharacters {
    [self.tableView setEditing:YES animated:YES];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                           target:self
                                           action:@selector(endTableEditing)];
    self.navigationItem.leftBarButtonItem = doneButton;
    [doneButton release];

    self.navigationItem.rightBarButtonItem = nil;
}

This method is triggered when a user selects an "Edit" button. You'll note that besides starting the editing, I also change around the button in my navbar. That's because when a user is editing I no longer need an "Edit" button, but instead require a "Done" button. You might alternatively set buttons' enabled properties to NO ... but in any case, you always need to give users a way to get out of editing mode.

Your tableView: method will probably delete the table item from your table and/or the data store that it originates from:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self recordWasDeleted:indexPath.row];
    }
}

For completeness state, I'll also include the method call generated when the "Done" button is clicked:

- (void)endTableEditing {
    [self.tableView setEditing:NO animated:YES];
    [self updateButtons];
}

This is pretty much the opposite of my editCharacters method: the editing is turned back off, then the buttons are returned to their original state.

And that's really all you need to do to edit a table: turn editing on, respond to the deletion message, and turn it back off at the end.

Doing More with Table Edits

You do have some other editing possibilities that I'm not going to cover in depth in this article. Most notably, you can adjust the editingStyle property of any individual cell. Apple claims it's set to UITableViewCellEditingStyleNone by default, but it sure looks to me like it's set to UITableViewCellEditingStyleDelete, which is what shows those handsome red minus marks. If you instead want to insert rows, you can set it to UITableViewCellEditingStyleInsert, and then do the appropriate thing when tableView:commitEditingStyle:forRowAtIndexPath: receives input of type insert.

That's it for me and tables for the nonce. If there's any other topics that you'd like covered regarding them, let me know in the comments. In the meantime, I'm planning to next return to my splashView topic, as some folks have requested in comments.


只有注册用户登录后才能发表评论。


网站导航: