我知道对此有很多问题,但我查看了每一个问题,却没有找到适合我的解决方案。我从教程中获得了用于编辑表格视图中行的位置的代码,它运行良好,但是每当我更改视图然后返回 tableView 页面(我认为发生 ViewDidAppear 方法)所有编辑的行回到他们之前的位置。这是我的代码:
与按钮相关的编辑操作:
- (void)editAction:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[[self tableView] setEditing:NO animated:NO];
[[self tableView] reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
[_tableData removeAllObjects];
}
else
{
[super setEditing:YES animated:YES];
[[self tableView] setEditing:YES animated:YES];
[[self tableView] reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
[_tableData removeAllObjects];
}
}
编辑方法:
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [self.datas objectAtIndex:fromIndexPath.row];
[self.datas removeObjectAtIndex:fromIndexPath.row];
[self.datas insertObject:item atIndex:toIndexPath.row];
}
// 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;
}
ViewDidAppear
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[_tableData removeAllObjects];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Data"];
self.datas = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [_searchDati objectAtIndex:indexPath.row];
} else {
NSString *string;
//configure cells
NSManagedObject *data = [self.datas objectAtIndex:indexPath.row];
string = [NSString stringWithFormat:@"%@", [data valueForKey:@"name"]];
[cell.detailTextLabel setText:nil];
cell.textLabel.text = string;
//tableData array used for filtering
[_tableData addObject:string];
}
return cell;
}
NSMutableArray tableData 仅用于UISearchbar
. 我无法弄清楚问题出在哪里。核心数据是相关的,@property (strong) NSMutableArray *datas;
所以我认为修改datas
数组应该没有问题,但也许这不是正确的事情。提前致谢。