如何让用户在UITableView中重新排序部分 [英] How to let the user reorder sections in a UITableView
问题描述
我正在开发一个有投资组合的应用程序。因此,这非常适合表视图,我正在进行编辑交互;它足够简单,允许用户添加或删除股票,在一个投资组合或其他投资组合中拖动它们,但有一件事我无法优雅地让用户将一个投资组合拖到另一个投资组合的上方或下方。 / p>
我现在有一个hacky解决方案,其中每个部分的第0行是投资组合名称,如果他们将该行拖到另一个投资组合之上,则会重新加载整个表随着投资组合的转换。这很有效,但感觉不太自然。
我确信我不是第一个遇到这个问题的人。任何人都有更精致的解决方案?
相关问题 - 如何让用户创建新的投资组合/部分?
简单易玩:
#importViewController.h
@interface ViewController()
@end
@implementation ViewController
{
NSMutableArray * _data;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//加载视图后进行任何其他设置,通常是从笔尖。
_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;
返回单元格;
}
- (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,然后在第二次点击时,在两个索引路径上调用该方法。它应该很好用,但你不会拖延它。
I'm working on an app with stocks, arranged in portfolios. So this is a natural fit for the table view, and I'm working on the editing interaction; it's straightforward enough to allow the user to add or delete stocks, drag them around within one portfolio or to another portfolio, but one thing that I haven't been able to do gracefully is let the user drag one portfolio above or below another.
I've got a hacky solution right now, where row 0 of each section is the portfolio name, and if they drag that row above another portfolio, the whole table is reloaded with the portfolios switched. This works, but doesn't feel very natural.
I'm sure I'm not the first to encounter this problem; anyone have a more refined solution?
A related question - how do I let users create a new portfolio/section?
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've asked for now is a little more complicated. I created an example that puts tables into cells, which gives you nested cells. The example is highly unattractive, but it works, and there's no reason you can't make it look pretty, so check it out:
https://github.com/MichaelSnowden/TableViewInCell
If that doesn't work for you, try making UITableView moveSection:(NSInteger) toSection:(NSInteger)
look pretty.
Documentation for that method is here.
My experience with the above method was that it's very easy to use, and it looks nice when it's called. A smart way to use it would be to create headers with tap gesture recognizers. On the first tap, highlight that section and record that indexPath, and on the second tap, call the method on the two index paths. It should work nicely, but you won't get drag-and-drop from it.
这篇关于如何让用户在UITableView中重新排序部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!