ios - 填充 TableView IOS 的单元格时出现 NSException

标签 ios objective-c json

我从 API 下载 JSON 响应并将其转换为 NSArray(“theArray”)。

JSON 格式如下所示

[{"RequestID":"1", "Title":"My Title"},{"RequestID":"1", "Title":"My Title"}]

转换发生在 ViewDidLoad 中

- (void)viewDidLoad {
    [super viewDidLoad];


    //JSON Request
    //Prepare the link
    NSURL * url = [[NSURL alloc] initWithString:@"getCategory.php"];

    //prepare the request object
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url  cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];

    [urlRequest setHTTPMethod:@"POST"];
    NSString *poststring = @"username=X";
    [urlRequest setHTTPBody:[poststring dataUsingEncoding:NSUTF8StringEncoding]];

    //prepare the variables for the json request
    NSData *urlData;
    NSURLResponse *response;
    NSError *error;

    //make the syncronous request
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];


    //construct an array arrount the data from the response
    NSMutableArray *object = [NSJSONSerialization JSONObjectWithData:urlData options:0 error:&error];

    theArray = [[NSMutableArray alloc] init];
    theArray = object;

}

但是,当调用以下方法来填充 TableView 的单元格时,出现 NSExceptions 错误

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSMutableArray *subArray = [theArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [subArray objectAtIndex:1];

    return cell;

}

起初我认为问题与数组中的无效指针有关,所以我将其分解为“子数组”,然后引用该数组索引 1 处的对象以获取值

错误如下:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x79fed110'
*** First throw call stack:
(
    0   CoreFoundation                      0x00af9a84 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x005bae02 objc_exception_throw + 50
    2   CoreFoundation                      0x00b02dd3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x00a40cdd ___forwarding___ + 1037
    4   CoreFoundation                      0x00a408ae _CF_forwarding_prep_0 + 14
    5   Document Collection 2               0x000e7ae0 -[ViewRequest tableView:cellForRowAtIndexPath:] + 288
    6   UIKit                               0x0100ea19 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
    7   UIKit                               0x0100eb47 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 90
    8   UIKit                               0x00fdd7f1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3317
    9   UIKit                               0x00ffdcdd __29-[UITableView layoutSubviews]_block_invoke + 52
    10  UIKit                               0x010188f3 -[UITableView _performWithCachedTraitCollection:] + 88
    11  UIKit                               0x00ffdbb2 -[UITableView layoutSubviews] + 214
    12  UIKit                               0x00f52eb7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
    13  libobjc.A.dylib                     0x005cf059 -[NSObject performSelector:withObject:] + 70
    14  QuartzCore                          0x048ee80a -[CALayer layoutSublayers] + 144
    15  QuartzCore                          0x048e24ee _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
    16  QuartzCore                          0x048e2352 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    17  QuartzCore                          0x048d4e8b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
    18  QuartzCore                          0x04908e03 _ZN2CA11Transaction6commitEv + 561
    19  QuartzCore                          0x0490a674 _ZN2CA11Transaction17flush_transactionEv + 50
    20  UIKit                               0x00eb9c8a _afterCACommitHandler + 197
    21  CoreFoundation                      0x00a1361e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    22  CoreFoundation                      0x00a1357e __CFRunLoopDoObservers + 398
    23  CoreFoundation                      0x00a08efc __CFRunLoopRun + 1340
    24  CoreFoundation                      0x00a08706 CFRunLoopRunSpecific + 470
    25  CoreFoundation                      0x00a0851b CFRunLoopRunInMode + 123
    26  GraphicsServices                    0x04188664 GSEventRunModal + 192
    27  GraphicsServices                    0x041884a1 GSEventRun + 104
    28  UIKit                               0x00e891eb UIApplicationMain + 160
    29  Document Collection 2               0x000e91aa main + 138
    30  libdyld.dylib                       0x03155a21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

subArray 不是 NSArray,它是 NSDictionary

试试这个:

NSDictionary *dict = theArray[indexPath.row];
cell.textLabel.text = dict[@"Title"];

编辑: 当你这样做的时候,删除这一行:

theArray = [[NSMutableArray alloc] init];

你在下一行设置它,设置两次是没有意义的。

关于ios - 填充 TableView IOS 的单元格时出现 NSException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33853476/

相关文章:

ios - AFNetworking 3.0 多部分请求

ios - MapView 不要求位置权限

ios - 是否可以使用 SpriteKit 创建复杂的平台游戏?

c# - 我可以反序列化为一个类并将额外的 JSON 属性放入该类的 JObject 中吗?

ios - iOS后台执行指南示例的Apple编程指南?

ios - swift subview 不会完全填充父 View

objective-c - 无法在 Swift 中使用 Sphero 碰撞检测?

ios - Google Maps IOS SDK 可以免费使用吗?

ruby-on-rails - Rails/Rspec JSON 整数在测试后调用时被转换为字符串

javascript - 在 Node.js 中获取错误的日期