How to allow users to reorder sections in a UITableView

I am working on an application with stocks located in portfolios. So this is a natural fit for the table view, and I'm working on editing; it's simple enough to allow the user to add or remove stocks, drag them in one portfolio or in another portfolio, but one thing that I couldn’t do gracefully is to allow the user to drag one portfolio higher or lower than the other.

I have a hacker solution right now, where line 0 of each section is the name of the portfolio, and if they drag this line over another portfolio, the whole table reloads when switching portfolios. It works, but not very natural.

I am sure that I am not the first to encounter this problem; who has a more sophisticated solution?

Related question - how do I allow users to create a new portfolio / section?

+8
ios uitableview order
source share
1 answer

Easy peasy:

#import "ViewController.h" @interface ViewController () @end @implementation ViewController { NSMutableArray *_data; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _data = [NSMutableArray arrayWithObjects:@"One", @"Two", @"Three", nil]; self.tableView.editing = YES; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _data.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"reuseIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.textLabel.text = _data[indexPath.row]; cell.showsReorderControl = YES; return cell; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { [_data exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; } @end 

EDIT:

What you asked now is a little more complicated. I created an example that puts tables in cells, which gives you nested cells. The example is very unattractive, but it works, and there is no reason why you cannot make it look beautiful, so check it out:

https://github.com/MichaelSnowden/TableViewInCell

If this does not work, try making UITableView moveSection:(NSInteger) toSection:(NSInteger) beautiful. The documentation for this method is here .

My experience with the above method was that it is very easy to use and it looks good when it called. A sensible way to use it would be to create headers using gesture recognizers. In the first touch, highlight this section and write down that indexPath, and in the second touch, call the method on two index tracks. It should work well, but you won’t get drag and drop from it.

+3
source share

All Articles