在我的表格 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];
}
如何确保在行移动之前完成此保存过程?也许这就是它不工作的原因,因为它比移动慢?!
你可以声明一个实例变量,比如:
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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |