菜鸟教程小白 发表于 2022-12-13 05:37:29

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


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

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

<pre><code>My two relevant functions:
-(void)textFieldDidEndEditing:(UITextField *)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 = ;
    MainCategory *mainCategory = ;

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

    self.activeField = nil;

    ;
}

- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
      toIndexPath:(NSIndexPath *)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&#39;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 = [ mutableCopy];

    // Grab the item we&#39;re moving
    NSManagedObject *resultToMove = ;

    // Remove the object we&#39;re moving from the array.
    ;
    // Now re-insert it at the destination.
    ];

    // All of the objects are now in their correct order. Update each
    // object&#39;s displayOrder field by iterating through the array.
    int i = 1;
    for (MainCategory *fetchedResult in fetchedResults)
    {
      fetchedResult.position = ;
    }

    // Save
    NSError *error = nil;
    ;

    // re-do the fetch so that the underlying cache of objects will be sorted
    // correctly
    ;
    ;

    self.reordering = NO;
}
</code></pre>

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

<pre><code>- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
      toIndexPath:(NSIndexPath *)destinationIndexPath;
{

    if(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&#39;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 = [ mutableCopy];

    // Grab the item we&#39;re moving
    NSManagedObject *resultToMove = ;

    // Remove the object we&#39;re moving from the array.
    ;
    // Now re-insert it at the destination.
    ];

    // All of the objects are now in their correct order. Update each
    // object&#39;s displayOrder field by iterating through the array.
    int i = 1;
    for (MainCategory *fetchedResult in fetchedResults)
    {
      fetchedResult.position = ;
    }

    // Save
    NSError *error = nil;
    ;

    // re-do the fetch so that the underlying cache of objects will be sorted
    // correctly
    ;
    ;

    self.reordering = NO;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.activeField = textField;
}

-(void)textFieldDidEndEditing:(UITextField *)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 = ;
//    MainCategory *mainCategory = ;
//
//    if(textField.text != mainCategory.name){
//      mainCategory.name = textField.text;
//    }
//   
//    self.activeField = nil;

    ;

//    ;
}

- (void)saveTextFieldEntry:(UITextField *)textField
{
    self.suspendAutomaticTrackingOfChangesInManagedObjectContext = YES;
    NSIndexPath *indexPath = ;
    MainCategory *mainCategory = ;

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

    self.activeField = nil;

    ;
}
</code></pre>

<p>如何确保在行移动之前完成此保存过程?也许这就是它不工作的原因,因为它比移动慢?!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你可以声明一个实例变量,比如:</p>

<pre><code>UITextField * selectedTextField;
</code></pre>

<p>并将其更改为:</p>

<pre><code>- (void)textFieldDidBeginEditing:(UITextField *)textField {
    selectedTextField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    selectedTextField = nil;
}
</code></pre>

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

<pre><code>if (selectedTextField &amp;&amp; )
      ;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS:文本字段委托(delegate),如何在处理表格 View 中的行移动之前自动结束编辑?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/20564294/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/20564294/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS:文本字段委托(delegate),如何在处理表格 View 中的行移动之前自动结束编辑?