我正在开发一个包含股票的应用程序,按投资组合排列。所以这很适合表格视图,我正在研究编辑交互;它足够简单,允许用户添加或删除股票,将它们拖动到一个投资组合或另一个投资组合中,但我无法优雅地做的一件事是让用户将一个投资组合拖动到另一个投资组合的上方或下方。
我现在有一个 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
EDIT:
你现在要求的有点复杂。我创建了一个将表格放入单元格的示例,这为您提供了嵌套单元格。这个例子非常没有吸引力,但它确实有效,而且你没有理由不能让它看起来很漂亮,所以检查一下:
https://github.com/MichaelSnowden/TableViewInCell https://github.com/MichaelSnowden/TableViewInCell
如果这对你不起作用,请尝试制作UITableView moveSection:(NSInteger) toSection:(NSInteger)
看起来不错。该方法的文档在这里 https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/moveSection%3atoSection%3a.
我对上述方法的经验是,它非常容易使用,而且调用时看起来很漂亮。一个聪明的使用方法是使用点击手势识别器创建标题。在第一次点击时,突出显示该部分并记录该索引路径,在第二次点击时,调用两个索引路径上的方法。它应该可以很好地工作,但是您不会从中进行拖放操作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)