What methods do I have to implement in order to re-arrange the UITableView rows?

后端 未结 5 1627
轻奢々
轻奢々 2021-02-04 18:43

For a simple example of using a NSMutableArray of strings called rows, what do I have to implement in my table controller to move the tableView rows and have the changes reflect

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 19:17

    Here we do our heavy lifting.

     - (void)tableView:(UITableView *)tableView 
    moveRowAtIndexPath:(NSIndexPath *)fromIndexPath 
           toIndexPath:(NSIndexPath *)toIndexPath 
    {
        NSLog(@"move from:%d to:%d", fromIndexPath.row, toIndexPath.row);
        // fetch the object at the row being moved
        NSString *r = [rows objectAtIndex:fromIndexPath.row]; 
    
        // remove the original from the data structure
        [rows removeObjectAtIndex:fromIndexPath.row];
    
        // insert the object at the target row
        [rows insertObject:r atIndex:toIndexPath.row];
        NSLog(@"result of move :\n%@", [self rows]);
    }
    

    Since this is a basic example, lets make all the rows moveable.

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

提交回复
热议问题