简体   繁体   中英

UITableView with Dragging

I have an iPad application with UITableView, and I want to enable a user to reorder the table items by dragging the cell and move it before or after another cell, how I can do that ?

Tanks

You need to implement these dataSource methods in your view controller:

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

and

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //update your model to reflect the fact that the specified rows indexes have been swapped
}

Implement moveRowAtIndexPath:toIndexPath: method in your data shource to update the model on the end of the drag operation. Implement tableView:canMoveRowAtIndexPath: to return YES for the rows that the user should be able to move. Here is a short tutorial on the subject.

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