Hitesh
Hitesh

Reputation: 1230

TableView drag & drop functionality

Can anyone please provide me a sample for table view drag & drop functionality?

Upvotes: 1

Views: 5458

Answers (2)

Narayanan Ramamoorthy
Narayanan Ramamoorthy

Reputation: 826

is that you need to rearrange ? . If so check this or if you need drag drop check this

Upvotes: 1

Filip Radelic
Filip Radelic

Reputation: 26683

When you create a subclass of UITableView controller, uncomment the following methods (or just add them if you are implementing UITableView delegate and dataSource protocols in custom class):

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    // update your model
}

// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}

Now you just need an edit button to start editing mode (you can put it in viewDidLoad):

self.navigationItem.rightBarButtonItem = self.editButtonItem;

And inside that tableView:moveRowAtIndexPath:toIndexPath: method you should update your model by rearranging array or whatever is holding your data.

Upvotes: 4

Related Questions