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

Apo*_*ace 10 uitableview ios

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

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

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

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

mic*_*den 3

十分简单:

#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
Run Code Online (Sandbox Code Playgroud)

编辑:

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

https://github.com/MichaelSnowden/TableViewInCell

如果这对你不起作用,请尝试让自己UITableView moveSection:(NSInteger) toSection:(NSInteger)看起来更漂亮。 该方法的文档位于此处

我对上述方法的经验是,它非常容易使用,而且调用时​​看起来很漂亮。一个聪明的使用方法是使用点击手势识别器创建标题。在第一次点击时,突出显示该部分并记录该索引路径,在第二次点击时,调用两个索引路径上的方法。它应该可以很好地工作,但是您不会从中进行拖放操作。