为什么滑动删除和拉动刷新导致崩溃?

时间:2015-01-22 20:00:55

标签: ios objective-c

NSDictionary *parameters = @{@"min_id": minID};中存在一个小错误。当我滑动删除然后拉到刷新时,应用程序崩溃。

这是我正在使用的代码:

- (void)requestNewItemsWithCompletionHandler:

(NewItemCompletionBlock)completionHandler {
    self.thereAreNoMoreOlderMessages = NO;
    if (self.isRefreshing == NO) {
        self.isRefreshing = YES;

        // Need to add images here

        NSString *minID = [[self.mediaItems firstObject] idNumber];
        NSDictionary *parameters = @{@"min_id": minID};

    [self populateDataWithParamaters:parameters completionHandler:^(NSError *error) {
            self.isRefreshing = NO;

            if (completionHandler) {
                completionHandler(error);
            }
        }];
    }
}

这是打印到控制台的错误消息:

2015-01-26 21:16:43.596 BlocstagramThird[43722:2760784] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000011087cf35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000110515bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000011078861f -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 383
    3   CoreFoundation                      0x000000011079b64b +[NSDictionary dictionaryWithObjects:forKeys:count:] + 59
    4   BlocstagramThird                    0x000000010ffd5012 -[DataSource requestNewItemsWithCompletionHandler:] + 322
    5   BlocstagramThird                    0x000000010ffda2bd -[ImagesTableViewController refreshControlDidFire:] + 173
    6   UIKit                               0x0000000110ead8be -[UIApplication sendAction:to:from:forEvent:] + 75
    7   UIKit                               0x0000000110fb4410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    8   UIKit                               0x0000000111598a29 -[UIRefreshControl _setRefreshControlState:notify:] + 318
    9   libdispatch.dylib                   0x0000000112ffcba6 _dispatch_call_block_and_release + 12
    10  libdispatch.dylib                   0x000000011301a7f4 _dispatch_client_callout + 8
    11  libdispatch.dylib                   0x0000000113001ede _dispatch_after_timer_callback + 334
    12  libdispatch.dylib                   0x000000011301a7f4 _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x000000011301109a _dispatch_source_latch_and_call + 852
    14  libdispatch.dylib                   0x00000001130096d2 _dispatch_source_invoke + 412
    15  libdispatch.dylib                   0x0000000113003771 _dispatch_main_queue_callback_4CF + 555
    16  CoreFoundation                      0x00000001107e4fe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    17  CoreFoundation                      0x00000001107a7eeb __CFRunLoopRun + 2043
    18  CoreFoundation                      0x00000001107a7486 CFRunLoopRunSpecific + 470
    19  GraphicsServices                    0x0000000114d909f0 GSEventRunModal + 161
    20  UIKit                               0x0000000110eac420 UIApplicationMain + 1282
    21  BlocstagramThird                    0x000000010ffd9113 main + 115
    22  libdyld.dylib                       0x000000011304f145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

这里是与滑动到删除相关的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        Media *item = [self items][indexPath.row];
        [[DataSource sharedInstance] deleteMediaItem:item];
    }
}

2 个答案:

答案 0 :(得分:1)

错误表示您尝试将nil对象(必须是基于代码示例的minID)添加到字典中,这是不允许的。

您可以在继续之前验证minID:

NSDictionary *parameters = nil;
if (self.mediaItems.count) {
    parameters = @{@"min_id": minID};

假设populateDataWithParameters方法可以接受nil参数。如果不能,你也需要保护那个电话。

答案 1 :(得分:-1)

//add this line to viewdidload method
self.tableView.allowsMultipleSelectionDuringEditing = NO;


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
 [aarname removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }    
}