moveRowAtIndexPath - Moving cells between sections

前端 未结 2 1857
花落未央
花落未央 2021-01-06 20:53

In my UITableView I have this said relationship

Department -< Employees (array of names)

I have set up custom objects for each model.

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 21:36

    Thanks to @Paulw11, the solution was that I did not account for the departmentFrom and departmentTo, as what I wanted was to Move Tom from Sales to Marketing;

    Thus the fix I have is:

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
    {
        if (fromIndexPath != toIndexPath ) {
    
            Department *departmentFrom = [_objects objectAtIndex:fromIndexPath.section];
            Department *departmentTo = [_objects objectAtIndex:toIndexPath.section];
    
            Employee *employee = [departmentFrom.employees objectAtIndex:fromIndexPath.row];
    
            [departmentFrom.employees removeObjectAtIndex:fromIndexPath.row];
            [departmentTo.employees insertObject:employee atIndex:toIndexPath.row];
            [tableView reloadData];
        }
    }
    

    Accepted Paulw11 answer.

提交回复
热议问题