【发布时间】: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