UITableView - 将人们从一个部分移动到另一个部分

时间:2015-02-18 21:09:48

标签: ios objective-c uitableview reorganize

我想创建一个tableview,我可以将不同部门的人员移动到其他部门,我在下面发布了一些代码。

我遇到了这个问题,我似乎永远无法让它获得通常的ui小工具来移动行。我不希望用户编辑/删除行;只需移动它们然而移动"移动"按钮似乎永远不会出现。

我做错了吗?

此外,我不确定我是否正在进行移动代码。

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Departments";

    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.tableView.allowsSelectionDuringEditing = YES;

    _objects = [NSMutableArray array];

    NSDictionary *sales = @{ @"name" : @"sales",
                             @"employees" : @[ @"Mike", @"Tom", @"Alex"] };

    NSDictionary *marketing = @{ @"name" : @"marketing",
                             @"employees" : @[ @"Heather", @"Richard", @"Simon"] };

    [_objects addObject:sales];
    [_objects addObject:marketing];


    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
}

#pragma mark - IBActions

-(IBAction) editButton:(id)sender
{
    [self setEditing:!self.editing animated:YES];
}

#pragma mark - UITableView delegate

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_objects count];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *department = [_objects objectAtIndex:section];
    NSArray *employees = department[@"employees"];
    return [employees count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];

    // Configure the cell...
    NSDictionary *department = [_objects objectAtIndex:indexPath.section];
    NSArray *employees = department[@"employees"];
    NSString *employeeName = [employees objectAtIndex:indexPath.row];
    cell.textLabel.text = employeeName;

    return cell;
}

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *department = [_objects objectAtIndex:section];
    return department[@"name"];
}

- (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath: (NSIndexPath *) indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    if( fromIndexPath == toIndexPath ) return;

    NSDictionary *department = [_objects objectAtIndex:fromIndexPath.section];
    NSArray *employees = department[@"employees"];
    NSString *employeeName = [employees objectAtIndex:fromIndexPath.row];

    [self.tableView beginUpdates];
    [_objects removeObjectAtIndex:fromIndexPath.row];
    [_objects insertObject:employeeName atIndex:toIndexPath.row];
    [self.tableView endUpdates];

    [tableView reloadData];
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    if (indexPath.section == 1 && [_objects count] > 1)
    {
        return YES;
    }
    return NO;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(editingStyle == UITableViewCellEditingStyleDelete){
        [_objects removeObjectAtIndex:indexPath.row];
        NSArray *rows = [NSArray arrayWithObject:indexPath];
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    }
}

1 个答案:

答案 0 :(得分:-1)

在初始化数组后调用此方法。 [self.tableView reloadData] 这将再次加载表数据。