尝试在UITableView中删除行时的NSExecption。

时间:2011-07-28 16:09:42

标签: iphone objective-c xcode ipad

其实我有一个购物车,我想添加一个“从购物车中删除一个项目”功能。但是当我尝试从表行中删除一个项目时,它会出现异常。至于据我所知,它与我的数组初始化有关。以下是例外和相关代码。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x4c45b00'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00ecc5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01020313 objc_exception_throw + 44
    2   CoreFoundation                      0x00ece0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00e3d966 ___forwarding___ + 966
    4   CoreFoundation                      0x00e3d522 _CF_forwarding_prep_0 + 50
    5   JewleryShop                         0x0000f7c8 -[BasketTableViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 158
    6   UIKit                               0x00427037 -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
    7   UIKit                               0x003bc4fd -[UIApplication sendAction:to:from:forEvent:] + 119
    8   UIKit                               0x0044c799 -[UIControl sendAction:to:forEvent:] + 67
    9   UIKit                               0x0044ec2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    10  UIKit                               0x0044d7d8 -[UIControl touchesEnded:withEvent:] + 458
    11  UIKit                               0x003e0ded -[UIWindow _sendTouchesForEvent:] + 567
    12  UIKit                               0x003c1c37 -[UIApplication sendEvent:] + 447
    13  UIKit                               0x003c6f2e _UIApplicationHandleEvent + 7576
    14  GraphicsServices                    0x01824992 PurpleEventCallback + 1550
    15  CoreFoundation                      0x00ead944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    16  CoreFoundation                      0x00e0dcf7 __CFRunLoopDoSource1 + 215
    17  CoreFoundation                      0x00e0af83 __CFRunLoopRun + 979
    18  CoreFoundation                      0x00e0a840 CFRunLoopRunSpecific + 208
    19  CoreFoundation                      0x00e0a761 CFRunLoopRunInMode + 97
    20  GraphicsServices                    0x018231c4 GSEventRunModal + 217
    21  GraphicsServices                    0x01823289 GSEventRun + 115
    22  UIKit                               0x003cac93 UIApplicationMain + 1160
    23  JewleryShop                         0x00002934 main + 102
    24  JewleryShop                         0x000028c5 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.

//When my basket gets the data.
NSMutableDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    if (dict)
    {
        //basketArray = [[NSMutableArray alloc] initWithObjects:nil];
        basketArray = [[dict objectForKey:@"myCartItems"] retain];
    }
//Code for deleting the table row data
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSUInteger count = [basketArray count];

    if (row < count) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSUInteger count = [basketArray count];

    if (row < count) {
        NSLog(@"row value is : %i",row);

        [basketArray removeObjectAtIndex:row];
    }
}
- (void)tableView:(UITableView *)tableView 
didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {


    [tableView reloadData];
}

2 个答案:

答案 0 :(得分:0)

当您尝试删除对象时,basketArray实际上并不是NSMutableArray,正如您在异常中看到的那样。它是一个NSArray,它不响应removeObjectAtIndex:。如果您希望basketArray是可变的,请检查您将其变为可变的位置,并确保它实际上正在执行。

答案 1 :(得分:0)

basketArray = [[NSMutableArray alloc] initWithObjects:nil]; 
basketArray = [[dict objectForKey:@"myCartItems"] retain];

您分配数组然后立即将其设置为从该字典中获取的对象。我认为正在发生的事情是你从字典中抓取的对象实际上是一个nsarray。

尝试而不是那两行

basketArray = [NSMutableArray arrayWithArray:[dict objectForKey:@"myCartItems"]];