• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOS:文本字段委托(delegate),如何在处理表格 View 中的行移动之前自动结束编辑?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:37:29 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的表格 View 的编辑阶段,您可以编辑所有单元格中的 UITextField 并移动单元格。如果您更改某个文本字段的文本并立即将该文本字段或包含该文本字段的单元格移动到另一个 indexPath,则会出现一个小问题,因为该文本尚未保存。然后一切都搞砸了。

为了解决这个问题,我基本上想在开始行移动之前自动调用 textFieldDidEndEditing。那应该有效吗?但是如何以编程方式正确结束文本字段的编辑?通过辞职第一响应者?我该怎么做?

My two relevant functions:
-(void)textFieldDidEndEditingUITextField *)textField
{
    self.suspendAutomaticTrackingOfChangesInManagedObjectContext = YES;
    //Did End editing is always called first after ending the editing either by enter or by clicking the done button. So this method saves the newly entered text
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath];

    if(textField.text != mainCategory.name){
        mainCategory.name = textField.text;
    }

    self.activeField = nil;

    [self stopSuspendAutomaticTrackingOfChangesInManagedObjectContextWithDelay:0.3f];
}

- (void)tableViewUITableView *)tableView
moveRowAtIndexPathNSIndexPath *)sourceIndexPath
      toIndexPathNSIndexPath *)destinationIndexPath;
{
    // Process the row move. This means updating the data model to correct the item indices.

    //reordering has been defined in the CoreDataViewController so the
    //FetchedResultsController doesn't mess up the reordering since he would update
    //the fetched results permanently while reordering
    self.reordering = YES;

    //Makes only a mutable copy of the array, but NOT the objects (references) within
    NSMutableArray *fetchedResults = [[self.fetchedResultsController fetchedObjects] mutableCopy];

    // Grab the item we're moving
    NSManagedObject *resultToMove = [self.fetchedResultsController objectAtIndexPath:sourceIndexPath];

    // Remove the object we're moving from the array.
    [fetchedResults removeObject:resultToMove];
    // Now re-insert it at the destination.
    [fetchedResults insertObject:resultToMove atIndex:[destinationIndexPath row]];

    // All of the objects are now in their correct order. Update each
    // object's displayOrder field by iterating through the array.
    int i = 1;
    for (MainCategory *fetchedResult in fetchedResults)
    {
        fetchedResult.position = [NSNumber numberWithInt:i++];
    }

    // Save
    NSError *error = nil;
    [self.budgetDatabase.managedObjectContext save:&error];

    // re-do the fetch so that the underlying cache of objects will be sorted
    // correctly
    [self.fetchedResultsController performFetch:&error];
    [self.tableView reloadData];

    self.reordering = NO;
}

编辑:我的新代码,仍然无法正常工作:

- (void)tableViewUITableView *)tableView
moveRowAtIndexPathNSIndexPath *)sourceIndexPath
      toIndexPathNSIndexPath *)destinationIndexPath;
{

    if(self.activeField){
        [self saveTextFieldEntry:self.activeField];
    }
    // Process the row move. This means updating the data model to correct the item indices.

    //reordering has been defined in the CoreDataViewController so the
    //FetchedResultsController doesn't mess up the reordering since he would update
    //the fetched results permanently while reordering
    self.reordering = YES;

    //Makes only a mutable copy of the array, but NOT the objects (references) within
    NSMutableArray *fetchedResults = [[self.fetchedResultsController fetchedObjects] mutableCopy];

    // Grab the item we're moving
    NSManagedObject *resultToMove = [self.fetchedResultsController objectAtIndexPath:sourceIndexPath];

    // Remove the object we're moving from the array.
    [fetchedResults removeObject:resultToMove];
    // Now re-insert it at the destination.
    [fetchedResults insertObject:resultToMove atIndex:[destinationIndexPath row]];

    // All of the objects are now in their correct order. Update each
    // object's displayOrder field by iterating through the array.
    int i = 1;
    for (MainCategory *fetchedResult in fetchedResults)
    {
        fetchedResult.position = [NSNumber numberWithInt:i++];
    }

    // Save
    NSError *error = nil;
    [self.budgetDatabase.managedObjectContext save:&error];

    // re-do the fetch so that the underlying cache of objects will be sorted
    // correctly
    [self.fetchedResultsController performFetch:&error];
    [self.tableView reloadData];

    self.reordering = NO;
}

-(void)textFieldDidBeginEditingUITextField *)textField
{
    self.activeField = textField;
}

-(void)textFieldDidEndEditingUITextField *)textField
{
//    self.suspendAutomaticTrackingOfChangesInManagedObjectContext = YES;
    //Did End editing is always called first after ending the editing either by enter or by clicking the done button. So this method saves the newly entered text
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
//    MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath];
//
//    if(textField.text != mainCategory.name){
//        mainCategory.name = textField.text;
//    }
//    
//    self.activeField = nil;

    [self saveTextFieldEntry:textField];

//    [self stopSuspendAutomaticTrackingOfChangesInManagedObjectContextWithDelay:0.3f];
}

- (void)saveTextFieldEntryUITextField *)textField
{
    self.suspendAutomaticTrackingOfChangesInManagedObjectContext = YES;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath];

    if(textField.text != mainCategory.name){
        mainCategory.name = textField.text;
    }

    self.activeField = nil;

    [self stopSuspendAutomaticTrackingOfChangesInManagedObjectContextWithDelay:0.3f];
}

如何确保在行移动之前完成此保存过程?也许这就是它不工作的原因,因为它比移动慢?!



Best Answer-推荐答案


你可以声明一个实例变量,比如:

UITextField * selectedTextField;

并将其更改为:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    selectedTextField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    selectedTextField = nil;
}

无论您在何处执行需要退出事件文本字段的操作,请添加:

if (selectedTextField && [selectedTextField isFirstResponder]) 
      [selectedTextField resignFirstResponder];

关于iOS:文本字段委托(delegate),如何在处理表格 View 中的行移动之前自动结束编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564294/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap