【问题标题】:Detail view not updating when UITableView rows are re-ordered重新排序 UITableView 行时,详细视图未更新
【发布时间】:2014-09-08 15:39:50
【问题描述】:

我有一个推送到 Detail ViewController 的 UITableView。在我的表格视图中,我的行是可拖动/可重新排列的。但是,当切换行时,选择行时路径保持不变并显示详细视图。示例:TableView Re-order Pic

如果我将“牛奶”行与“健身房”行切换,然后选择“健身房”,详细视图仍会返回牛奶行的详细信息。

我该如何解决这个问题,以便行的路径可以适当地切换?

.m :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self->newArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *theFood = [values objectAtIndex:indexPath.row];
    cell.textLabel.text = [theFood valueForKey:@"name"];
    cell.detailTextLabel.text = [theFood valueForKey:@"price"];

    return cell;
}



- (IBAction) EditTable:(id)sender
{
    if(self.editing)
    {
        [super setEditing:NO animated:NO];
        [_myTableView setEditing:NO animated:NO];
       // [_myTableView reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
    }
    else
    {
        [super setEditing:YES animated:YES];
        [_myTableView setEditing:YES animated:YES];
       // [_myTableView reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];

    }
}


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

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
        if( fromIndexPath == toIndexPath ) {
            return;
        }

    NSDictionary *moveFood = [self->newArray objectAtIndex:fromIndexPath.row];
    [self->newArray removeObjectAtIndex:fromIndexPath.row];
    [self->newArray insertObject:moveFood atIndex:toIndexPath.row];



}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"detailSegue"]) {
        NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow];
        DetailViewController *detailVC = segue.destinationViewController;
        detailVC.detailFood = [values objectAtIndex:indexPath.row];

    }
}

【问题讨论】:

  • 您在 TableView 的“后面”使用什么数据结构。你不能更新一下然后reloadData吗?
  • (将 TableView 视为表的“视图”。您管理表,然后让 TableView “查看”它。您不使用 TableView 作为数据结构。)
  • 所以,更新您的values 数组和reloadData
  • 我在 moveRowAtIndexPath 方法的末尾尝试了 [_myTableView reloadData],但它似乎不起作用.. :(
  • 你更新values了吗?您是否从values(更新后)获取详细视图的信息? (您上面的代码正在更新newArray,但不是values。)

标签: ios uitableview didselectrowatindexpath nsindexpath


【解决方案1】:

当您切换行时,您还应该更新您的数据源。否则当调用 tableView 的 didSelect 方法时,它会访问数据源数组中的错误对象。

所以在–tableView:moveRowAtIndexPath:toIndexPath: switch 中切换的对象的位置。

【讨论】:

    【解决方案2】:

    特别感谢@HotLicks 帮助我!

    问题是在我的tableView:moveRowAtIndexPath:toIndexPath: 方法更新之后,我没有将详细视图的数据源从 values NSArray 更改为我的 newArray NSMutableArray!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 2011-06-20
      • 2013-10-10
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多