如何让用户在UITableView中重新排序部分

我正在开发一个有股票的应用程序,安排在投资组合中。 因此,这非常适合表视图,我正在进行编辑交互; 它足够简单,允许用户添加或删除股票,在一个投资组合或其他投资组合中拖动它们,但有一件事我无法优雅地让用户将一个投资组合拖到另一个投资组合的上方或下方。

我现在有一个hacky解决方案,每个部分的第0行是投资组合名称,如果他们将该行拖到另一个投资组合之上,则整个表格会重新加载投资组合。 这有效,但感觉不是很自然。

我敢肯定我不是第一个遇到这个问题的人; 谁有更精致的解决方案?

相关问题 – 如何让用户创建新的投资组合/部分?

十分简单:

#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 

编辑:

你现在要求的是一点点复杂。 我创建了一个将表放入单元格的示例,它为您提供嵌套单元格。 这个例子非常缺乏吸引力,但它很有效,并且没有理由你不能让它看起来很漂亮,所以检查出来:

https://github.com/MichaelSnowden/TableViewInCell

如果这对您不起作用,请尝试制作UITableView moveSection:(NSInteger) toSection:(NSInteger)看起来很漂亮。 该方法的文档在这里 。

我使用上述方法的经验是它非常容易使用,并且在调用它时看起来很好。 使用它的一种聪明方法是使用轻敲手势识别器创建标题。 在第一次点击时,突出显示该部分并记录该indexPath,然后在第二次点击时,在两个索引路径上调用该方法。 它应该很好地工作,但你不会从它拖放。