简体   繁体   中英

UITableView - move people from one section to another

I would like to create a tableview where I can move people from different departments into other departments, and I have some code posted below.

I have an issue with this, I can never seem to get it to get the usual ui gadget to move rows. I don't want the user to edit/delete the rows; simply move them about however the "move" buttons never seem to appear.

Is there something I am doing wrong?

Also I am not sure if I am doing the move code right.

@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];
    }
}

call this method after your array initialised. [self.tableView reloadData] This will load the table data once again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM