如何使UITableView重新排列? [英] How to make UITableView rearrangeable?

查看:235
本文介绍了如何使UITableView重新排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的UITableView可编辑,以便您可以移动单元格。现在当我点击编辑按钮时,它只允许我删除但不能重新安排。

I'm trying to make my UITableView editable so you can move the cells around. Right now when I click the edit button, it only lets me delete but not re-arrange.

我的方法是:

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

- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
    [self.routineTableView setEditing: !self.routineTableView.editing animated:YES];

    if (self.routineTableView.editing)
        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
    else
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
     if (editingStyle == UITableViewCellEditingStyleDelete) 
     {
         NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
         [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
         NSLog(@"fetched results : \n%@\n",[self.fetchedResultsController fetchedObjects]);

         NSError *error = nil;

         if (![managedObjectContext save:&error]) 
         {
             // Handle the error.
         }
    }

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


推荐答案

为了实现重新排序行正确地说,表视图数据源应该实现如下方法:

In order to implement reordering rows correctly, the table view data source should implement methods like :

-(BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
       toIndexPath:(NSIndexPath *)toIndexPath;

及其代表可以实施:

-(NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
                     toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;

还有这个页面为您提供代码示例。

There is also this page giving you examples of code.

这篇关于如何使UITableView重新排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆