【问题标题】:IOS App crashes - unrecognized selector sent to instanceIOS 应用程序崩溃 - 无法识别的选择器发送到实例
【发布时间】:2016-02-13 17:55:15
【问题描述】:

在 tableView:cellForRowAtIndexpath 方法中,我的代码如下。

当我注释掉包含在双星号 (*) 中的 if 条件时,应用程序会崩溃,但会出现下面提到的异常。如果我取消注释它,那么它工作正常。请帮忙,因为我是 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;

}

抛出的异常是:

-[__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
...    
)

编辑: invoiceList 的 NSLog:

{
    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;
            }
        )
    );
}

【问题讨论】:

  • indexPath+1 条件的目的是什么? invoiceList 是您的数据数组吗?也发布您的numberOfRows 方法代码。
  • 您确定异常确实发生在您向我们展示的 objectAtIndex 行吗?预先打印 invoiceList 的类:NSLog(@"%@",[invoiceList class])
  • 能否提供发票清单NSLog
  • 大家好,indexPath+1条件没有特殊用途。但是,在这种情况下,代码不会抛出任何异常并且可以完美运行。我还在 EDIT 部分提供了 invoiceList NSLog。感谢您的支持。
  • 上面带有UITableView的代码是写在First ViewController上的。当用户在 UITableView 中选择一行并按下 Next 按钮时,其详细信息将显示在第二个 ViewController 上。假设只选择了一行并单击了 Next 按钮,则调用第二个 ViewController 的 viewDidLoad 方法,一旦完成,运行时也会调用第一个 ViewController 的 cellForRowAtIndexPath。此时,它抛出异常并且 numberOfRowsInSection 方法返回 12 代替 1。我无法理解为什么 numberOfRowsInSection 返回 12。它应该返回 1。请帮忙。

标签: ios objective-c iphone uitableview exception


【解决方案1】:

您正在检查 if (invoiceList.count == indexPath.row + 1) 但不是它,而是使用 if (invoiceList.count> indexPath.row )。另请检查“numberOfRowsInIndexpath”是否作为 invoiceList.count 返回。

【讨论】:

  • numberOfRowsInIndexpath 正在返回 invoiceList.count。但问题是如果没有注释 if 条件,那么相同的代码可以正常工作。
  • invoiceList 是这个字典还是数组?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多