IOS App crashes - unrecognized selector sent to instance

时间:2015-11-12 11:59:24

标签: ios objective-c iphone uitableview exception

In tableView:cellForRowAtIndexpath method, my code is as below.

When I comment out the if condition enclosed in double asterisk(*), the app crashes with the exception mentioned below. And if I uncomment it then it works fine. Please help as I am new to Objective-C.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @“MyTableCell";

    cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@“MyTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    **if (invoiceList.count == indexPath.row  + 1)**
    {
        NSMutableDictionary *currentDictionary = [invoiceList objectAtIndex:indexPath.row];        
        cell.invoiceDetails = currentDictionary;

        for ( NSString *key in [cell.invoiceDetails allKeys]) {
            NSString *value = [cell.invoiceDetails valueForKey:key];

            if([key isEqualToString:MERCHANT_NAME]){
                cell.merchantNameValue.text  = (value == (id)[NSNull null]) ? @"" : value;
            }

            if([key isEqualToString:INVOICE_ID]){
                cell.invoiceIdValue.text  = (value == (id)[NSNull null]) ? @"" : value;
            }

            if([key isEqualToString:TOTAL_AMOUNT]){
                cell.totalAmountValue.text  = (value == (id)[NSNull null]) ? @"" : value;
            }
        }
    }

    return cell;

}

Exception thrown is:

-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance 0x7fc2a604e9f0
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance 0x7fc2a604e9f0'

*** First throw call stack:

(
0   CoreFoundation                      0x000000010fbd1f35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010f63ebb7 objc_exception_throw + 45
2   CoreFoundation                      0x000000010fbd904d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x000000010fb3127c ___forwarding___ + 988
4   CoreFoundation                      0x000000010fb30e18 _CF_forwarding_prep_0 + 120
5   MobileWalletBanking                 0x000000010cf0037f -[MyViewController tableView:cellForRowAtIndexPath:] + 479
6   UIKit                               0x000000010dd9b4b3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
7   UIKit                               0x000000010dd7afb1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
8   UIKit                               0x000000010dd90e3c -[UITableView layoutSubviews] + 213
9   UIKit                               0x000000010dd1d973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
10  QuartzCore                          0x000000010d2bade8 -[CALayer layoutSublayers] + 150
...    
)

EDIT: NSLog of invoiceList:

{
    501 = 72;
    610 = Merchant1;
    611 = Merchant1;
    612 = 51573;
    615 = 51573;
    616 = "07/08/2015 00:00:00";
    617 = "64.01";
    618 = 498;
    619 = 498;
    662 =     (
                {
            501 = 72;
            520 = "15.18";
            615 = 51573;
            616 = "07/08/2015 00:00:00";
            620 = 43;
            621 = "15.18";
            623 = True;
            624 = 43;
            740 = “Text1";
        },
                {
            501 = 72;
            520 = 0;
            615 = 51573;
            616 = "07/08/2015 00:00:00";
            620 = 10;
            621 = 0;
            623 = True;
            624 = 10;
            740 = “Text2";
        },
                {
            501 = 72;
            520 = "37.63";
            615 = 51573;
            616 = "07/08/2015 00:00:00";
            620 = 2;
            621 = "37.63";
            623 = True;
            624 = 2;
            740 = “Text3";
        },
                {
            501 = 72;
            520 = "11.20";
            615 = 51573;
            616 = "07/08/2015 00:00:00";
            620 = 3;
            621 = "11.20";
            623 = True;
            624 = 3;
            740 = “Text4";
        },
                {
            501 = 72;
            520 = 0;
            615 = 51573;
            616 = "07/08/2015 00:00:00";
            620 = 14;
            621 = 0;
            623 = True;
            624 = 14;
            740 = “Text5";
        }
    );
    663 =     (
                (
                        {
                Key = 501;
                Value = 83;
            },
                        {
                Key = Name;
                Value = "INVOICE_CONNECTION_PROPERTIES";
            },
                        {
                Key = 611;
                Value = Merchant1;
            },
                        {
                Key = 610;
                Value = "";
            },
                        {
                Key = 615;
                Value = 51573;
            },
                        {
                Key = 620;
                Value = 1;
            },
                        {
                Key = "SERVICE_NAME";
                Value = “Text1";
            },
                        {
                Key = "FIELD_1";
                Value = 4272035;
            },
                        {
                Key = "FIELD_1_CONNECTION_FIELD_ID";
                Value = 000000000000481;
            },
                        {
                Key = "FIELD_1_QS_NAME";
                Value = "Id";
            },
                        {
                Key = "FIELD_1_QS_VALUE";
                Value = "Id";
            },
                        {
                Key = "FIELD_2";
                Value = 10;
            },
                        {
                Key = "FIELD_2_CONNECTION_FIELD_ID";
                Value = 000000000000482;
            },
                        {
                Key = "FIELD_2_QS_NAME";
                Value = "index1";
            },
                        {
                Key = "FIELD_2_QS_VALUE";
                Value = "index1";
            },
                        {
                Key = "FIELD_3";
                Value = "";
            },
                        {
                Key = "FIELD_3_IS_MODIFIED";
                Value = 1;
            },
                        {
                Key = "FIELD_3_CONNECTION_FIELD_ID";
                Value = 000000000000483;
            },
                        {
                Key = "FIELD_3_QS_NAME";
                Value = "index2";
            },
                        {
                Key = "FIELD_3_QS_VALUE";
                Value = "index2";
            },
                        {
                Key = "FIELD_4";
                Value = R;
            },
                        {
                Key = "FIELD_4_CONNECTION_FIELD_ID";
                Value = 000000000000488;
            },
                        {
                Key = "FIELD_4_QS_NAME";
                Value = "index_Type";
            },
                        {
                Key = "FIELD_4_QS_VALUE";
                Value = "Index_Type";
            }
        ),
                (
                        {
                Key = 501;
                Value = 83;
            },
                        {
                Key = Name;
                Value = "INVOICE_CONNECTION_PROPERTIES";
            },
                        {
                Key = 611;
                Value = Merchant1;
            },
                        {
                Key = 610;
                Value = "";
            },
                        {
                Key = 615;
                Value = 51573;
            },
                        {
                Key = 620;
                Value = 2;
            },
                        {
                Key = "SERVICE_NAME";
                Value = “Text2";
            },
                        {
                Key = "FIELD_1";
                Value = 4272033;
            },
                        {
                Key = "FIELD_1_CONNECTION_FIELD_ID";
                Value = 000000000000481;
            },
                        {
                Key = "FIELD_1_QS_NAME";
                Value = "Id";
            },
                        {
                Key = "FIELD_1_QS_VALUE";
                Value = "Id";
            },
                        {
                Key = "FIELD_2";
                Value = 3;
            },
                        {
                Key = "FIELD_2_CONNECTION_FIELD_ID";
                Value = 000000000000482;
            },
                        {
                Key = "FIELD_2_QS_NAME";
                Value = "index1";
            },
                        {
                Key = "FIELD_2_QS_VALUE";
                Value = "index1";
            },
                        {
                Key = "FIELD_3";
                Value = "";
            },
                        {
                Key = "FIELD_3_IS_MODIFIED";
                Value = 1;
            },
                        {
                Key = "FIELD_3_CONNECTION_FIELD_ID";
                Value = 000000000000483;
            },
                        {
                Key = "FIELD_3_QS_NAME";
                Value = "index2";
            },
                        {
                Key = "FIELD_3_QS_VALUE";
                Value = "index2";
            },
                        {
                Key = "FIELD_4";
                Value = C;
            },
                        {
                Key = "FIELD_4_CONNECTION_FIELD_ID";
                Value = 000000000000488;
            },
                        {
                Key = "FIELD_4_QS_NAME";
                Value = "index_type";
            },
                        {
                Key = "FIELD_4_QS_VALUE";
                Value = "index_type";
            }
        )
    );
    664 =     (
                (
                        {
                Key = 501;
                Value = 73;
            },
                        {
                Key = 625;
                Value = 000000000000481;
            },
                        {
                Key = 611;
                Value = Merchant1;
            },
                        {
                Key = 626;
                Value = "id";
            },
                        {
                Key = 627;
                Value = 1;
            },
                        {
                Key = 590;
                Value = True;
            }
        ),
                (
                        {
                Key = 501;
                Value = 73;
            },
                        {
                Key = 625;
                Value = 000000000000482;
            },
                        {
                Key = 611;
                Value = Merchant1;
            },
                        {
                Key = 626;
                Value = "index1";
            },
                        {
                Key = 627;
                Value = 2;
            },
                        {
                Key = 590;
                Value = True;
            }
        ),
                (
                        {
                Key = 501;
                Value = 73;
            },
                        {
                Key = 625;
                Value = 000000000000483;
            },
                        {
                Key = 611;
                Value = Merchant1;
            },
                        {
                Key = 626;
                Value = "index2";
            },
                        {
                Key = 627;
                Value = 3;
            },
                        {
                Key = 590;
                Value = True;
            }
        ),
                (
                        {
                Key = 501;
                Value = 73;
            },
                        {
                Key = 625;
                Value = 000000000000488;
            },
                        {
                Key = 611;
                Value = Merchant1;
            },
                        {
                Key = 626;
                Value = "index_type";
            },
                        {
                Key = 627;
                Value = 4;
            },
                        {
                Key = 590;
                Value = True;
            }
        )
    );
}

1 个答案:

答案 0 :(得分:0)

You are checking if (invoiceList.count == indexPath.row + 1) but instead of it, use if (invoiceList.count> indexPath.row ). Please also check "numberOfRowsInIndexpath" is returning as invoiceList.count.