【问题标题】:UITableView reordering cells in UIViewController - reorder control not showingUIViewController 中的 UITableView 重新排序单元格 - 重新排序控件未显示
【发布时间】:2012-11-19 22:58:32
【问题描述】:

我正在尝试使用 2 类 UITableViewCell 的 UITableView(第 0 节有一行,第 1 节有 [self.dataArray count] 行)。

我尝试了这些链接: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageReorderRow/ManageReorderRow.html

reordering control in UITableView
Reorder control isn't displayed on table view

我已添加:

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{   

    // Get the object we are going to move
    Task *task = [[Task alloc] init];
    task = [self.taskArray  objectAtIndex:[destinationIndexPath row]];
    
    // We will delete and move it to another location so well have to retain it
    //[task retain];
    
    // Remove it an move it to other place
    [self.taskArray removeObjectAtIndex:[sourceIndexPath row]];
    [self.taskArray insertObject:task atIndex:[destinationIndexPath row]];
}


     // Override to support conditional rearranging of the table view.
     - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
         if (indexPath.section == 1) {
             return YES;
         }
         return YES;
     }

我确实为每个 UITableViewCell 类添加了:

cell.showsReorderControl = YES;

但仍然没有运气,没有显示 readorderControl ......有人吗?有这么多教程,但对我不起作用,这非常紧迫和烦人。

编辑:

我更改了一点主题以包含事实,即我使用的是 UIViewController 而不是 UITableViewController。

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    要在UITableView 中实现行重新排序,您需要实现tableView:canMoveRowAtIndexPath:tableView:moveRowAtIndexPath:toIndexPath:,您可能还需要实现tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:

    最重要的是,表格视图必须处于编辑模式才能显示重新排序控件。

    【讨论】:

    • 我已添加到我的 navigationItem.rightbuttons [self editButtonItem]; 但按下并没有启动拖动控制器出现 - 知道吗?我不想一直将表格设置为编辑模式
    • 我的意思是,除了使用附加方法构建自定义按钮之外 - 是否有适当或更好的方法来实现这一目标?
    • 添加[self editButtonItem] 很好,前提是您使用的是UITableViewController 而不是UIViewController。您是否有机会覆盖 setEditing:animated: 方法?如果是,请务必致电[super setEditing:editing animated:animated]'
    • 谢谢,你救了我的命 :) 看起来 SO 上的每个人都在使用 UITableViewController,所以他们没有添加这个小细节 :)
    • 您仍然可以在UIViewController 中使用[self editButtonItem]。您只需确保覆盖 setEditing:animated: 以包含对 [self.tableView setEditing:editing animated:animated] 的调用。
    【解决方案2】:

    试试这个。 . .这将在编辑模式下的简单表格视图的情况下处理单元格的排列和更新

    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
      return YES;
    }
    
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
         [tableData insertObject: [tableData objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
         [tableData removeObjectAtIndex:(sourceIndexPath.row + 1)];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      相关资源
      最近更新 更多