繁体   English   中英

删除一行将导致应用结束; 没有控制台输出

[英]Deleting a row causes app to end; no console output

我有一个UITableViewController,里面有一堆随机项。 我希望能够向其中添加项目,并从中删除项目。 我在第一部分的标题中有一个UITextField,因此您可以键入一个单词/短语并将其添加到列表中。

您可以毫无问题地从列表中删除单词,但是,如果将单词添加到列表中,然后尝试将其删除,则应用程序将结束并返回主屏幕; 控制台中没有任何内容。

我不确定问题出在哪里。 这是我一直在使用的addNewWord方法:

- (void)addNewWord
{
    NSString *newWord = [textField text];
    [array insertObject:newWord atIndex:0];
    NSLog(@"%d", [array count]);
    [textField setText:@""];

    [[self tableView] reloadData];

}

这是-(void)tableView:commitEditingStyle:forRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [array removeObjectAtIndex:[indexPath row]];
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationRight];
        [tableView endUpdates];
    }
    NSLog(@"%d", [array count]);
}

这就是我可能要解决的问题的全部原因,这也是我被困的原因。

非常感谢任何见解。 非常感谢你!

-编辑:添加了[tableView beginUpdates]; 和[tableView endUpdates]; 相同的结果。 还有其他想法吗?

-Edit2:当我添加[tableView beginUpdates]时; 和[tableView endUpdates] ;,我得到一个例外。 这是控制台输出:

2010-09-30 05:56:40.172 DictionaryWordsTest[2350:207] *** Assertion failure in 
-[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/
UIKit-1262.60.3/UITableView.m:920
2010-09-30 05:56:40.175 DictionaryWordsTest[2350:207] *** Terminating app 
due to uncaught exception 'NSInternalInconsistencyException', reason: 
'Invalid update: invalid number of rows in section 0.  The number of rows 
contained in an existing section after the update (17) must be equal to the 
number of rows contained in that section before the update (17), 
plus or minus the number of rows inserted or deleted from that section 
(0 inserted, 1 deleted).'
*** Call stack at first throw:
(
0   CoreFoundation                      0x02486b99 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x025d640e objc_exception_throw + 47
2   CoreFoundation                      0x0243f238 +[NSException raise:format:arguments:] + 136
3   Foundation                          0x000b2e37 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4   UIKit                               0x00332d37 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8719
5   UIKit                               0x00322a01 -[UITableView endUpdates] + 42
6   DictionaryWordsTest                 0x00002a73 -[DictionaryViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 140
7   UIKit                               0x0031f96d -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
8   UIKit                               0x002b87f8 -[UIApplication sendAction:to:from:forEvent:] + 119
9   UIKit                               0x00343de0 -[UIControl sendAction:to:forEvent:] + 67
10  UIKit                               0x00346262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
11  UIKit                               0x00344e0f -[UIControl touchesEnded:withEvent:] + 458
12  UIKit                               0x002dc3d0 -[UIWindow _sendTouchesForEvent:] + 567
13  UIKit                               0x002bdcb4 -[UIApplication sendEvent:] + 447
14  UIKit                               0x002c29bf _UIApplicationHandleEvent + 7672
15  GraphicsServices                    0x02d66822 PurpleEventCallback + 1550
16  CoreFoundation                      0x02467ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
17  CoreFoundation                      0x023c8807 __CFRunLoopDoSource1 + 215
18  CoreFoundation                      0x023c5a93 __CFRunLoopRun + 979
19  CoreFoundation                      0x023c5350 CFRunLoopRunSpecific + 208
20  CoreFoundation                      0x023c5271 CFRunLoopRunInMode + 97
21  GraphicsServices                    0x02d6500c GSEventRunModal + 217
22  GraphicsServices                    0x02d650d1 GSEventRun + 115
23  UIKit                               0x002c6af2 UIApplicationMain + 1160
24  DictionaryWordsTest                 0x00001f58 main + 102
25  DictionaryWordsTest                 0x00001ee9 start + 53
)
terminate called after throwing an instance of 'NSException'

您需要使用[tableView beginUpdates]和[tableView endUpdates]包装-[tableView deleteRowsAtIndexPaths]调用。

奇怪的是,答案最终超出了我的预期。 我不确定有什么区别,为什么我没有控制台输出,但这是我最终要做的。

在addNewWord中,我没有使用reloadData,而是使用以下方法重新加载tableView,并在顶部插入单词(因此,不必滚动到视图的底部即可看到新单词):

- (void)addNewWord
{
[[self tableView] beginUpdates];
NSString *newWord = [textField text];

[array insertObject:newWord atIndex:0];
[textField setText:@""];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[[self tableView] endUpdates];
}

然后,我认为关键是,不会从以下方式调用addNewWord:

[textField addTarget:self action:@selector(addNewWord) forControlEvents:UIControlEventEditingDidEndOnExit]; 

但是,我在文本字段旁边添加了一个调用它的按钮。 现在一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM