views:

40

answers:

1

Apple's documentation on UITableView discusses how to add/edit/delete rows, but it doesn't discuss adding/editing/deleting sections is detail, specially with respect to the UI. In case of add/delete rows, you can apply "add" and "delete" styles on the row using:

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

However, there is no such method for sections (that i know of), which can be used to show visual controls to the user for adding "+" or deleting "-" sections. I can add a new section using the logic used for new row, however, I'm not sure how to go about deleting the whole section (or allowing the user to edit section e.g. section heading). How should i go about deleting the whole section?

Background: I'm working on a Core Data based application. I have a one to many relationship between entities.

Entity r (root)   <-->> Entity p (parent),
Entity p (parent) <-->> Entity c (child)

When user creates a parent record (entity p), fixed set of child records are automatically created (entity c). In the UI, I'm showing basic information of parent (entity p) in the header e.g. title, and the associated child records (entity c) as section rows, and some summary in the footer. For some logical reasons, i can't opt for drill-down approach (to avoid adding/removing sections).

+1  A: 

Edited:

Since you can define custom views for sections, your best bet is to replace the existing one with your own UIView which, when swiped (using gestures) would delete the section. You could probably imitate that red delete button to the right, or you could just have a UIAlertView with confirmation when the user swipes on a section.

You'd use tableView:deleteSections:withRowAnimation: to delete the section.

The following SO question delves into the subject: http://stackoverflow.com/questions/1061071/uitableview-deleting-sections-with-animation

Was:

Firstly,

// UITableView.h:311...
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

Secondly,

UIKIT_EXTERN_CLASS @interface UITableView : UIScrollView <NSCoding> {
// ... 
NSInteger                   _swipeToDeleteSection;
Kalle
I'm aware of the UITableView methods you've referred, and i understand the logic which will be involved in deleting a section or adding a new section. And, _swipeToDeleteSection is a private member of the UITableView class. It's not exposed in the API documentation, so i'm guessing that even if i figure out how to use it, my application won't get accepted.
Mustafa
Ah, hrm. So not getting you closer at all then... at least the functionality seems to exist. I'm guessing Apple put it private because they decided not to support it or something.
Kalle
Wait, nevermind, there is a tableView:deleteSections:withRowAnimation: method, apparently. But I guess your problem is getting it to support swiping to delete the section.
Kalle
Replaced answer above. Hopefully it's helpful this time around. ;)
Kalle