【问题标题】:Add more items to UITableView during runtime在运行时向 UITableView 添加更多项目
【发布时间】:2015-08-15 15:03:39
【问题描述】:

我目前正在尝试分页 Twitter 提要,但是当我尝试将分页时收到的新项目添加到现有的 NSMutableArray 时,我收到了以下错误:

Floadt[28482:2207263] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106263c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001059a0bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000106263b9d +[NSException raise:format:] + 205
    3   CoreFoundation                      0x000000010625c46a -[__NSCFArray insertObject:atIndex:] + 106
    4   CoreFoundation                      0x0000000106186923 -[NSMutableArray insertObjects:count:atIndex:] + 179
    5   CoreFoundation                      0x0000000106186654 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 372
    6   CoreFoundation                      0x0000000106186454 -[NSMutableArray addObjectsFromArray:] + 612
    7   Floadt                              0x0000000102375d89 __57-[TwitterTableViewController fetchNextTwitterPageWithID:]_block_invoke + 217
    8   Floadt                              0x0000000102348e43 __64-[AFJSONRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke91 + 51
    9   libdispatch.dylib                   0x000000010844d186 _dispatch_call_block_and_release + 12
    10  libdispatch.dylib                   0x000000010846c614 _dispatch_client_callout + 8
    11  libdispatch.dylib                   0x0000000108454a1c _dispatch_main_queue_callback_4CF + 1664
    12  CoreFoundation                      0x00000001061cb1f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    13  CoreFoundation                      0x000000010618cdcb __CFRunLoopRun + 2043
    14  CoreFoundation                      0x000000010618c366 CFRunLoopRunSpecific + 470
    15  GraphicsServices                    0x0000000107d4ba3e GSEventRunModal + 161
    16  UIKit                               0x00000001046d4900 UIApplicationMain + 1282
    17  Floadt                              0x000000010242d46f main + 111
    18  libdyld.dylib                       0x00000001084a0145 start + 1
    19  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

分页的 JSON 完全正常返回。当用户滚动到 TableView 的底部时,我只想在原始 Items 下方添加分页项。

检索分页推文的方法

-(void)fetchNextTwitterPageWithID:(NSString *)objectID {
    self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"tA5TT8uEtg88FwAHnVpBcbUoq" secret:@"L5whWoi91HmzjrE5bNPNUgoMXWnImvpnkIPHZWQ4VmymaoXyYV"];

    NSDictionary *parameters = @{
                                 @"max_id" :objectID
                                 };

    AFOAuth1Token *twitterToken = [AFOAuth1Token retrieveCredentialWithIdentifier:@"TwitterToken"];
    [self.twitterClient setAccessToken:twitterToken];
    [self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self.twitterClient getPath:@"statuses/home_timeline.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSMutableArray *responseArray = (NSMutableArray *)responseObject;
         NSLog(@"Response: %@", responseObject);
         tweets = [tweets copy];
         [tweets addObjectsFromArray:responseArray];
         [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

查找 Twitter 用户的方法

- (void)lookupTwitterUser:(NSString *)user {
    self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"tA5TT8uEtg88FwAHnVpBcbUoq" secret:@"L5whWoi91HmzjrE5bNPNUgoMXWnImvpnkIPHZWQ4VmymaoXyYV"];

    NSDictionary *parameters = @{
                                 @"screen_name" :user
                                 };

    AFOAuth1Token *twitterToken = [AFOAuth1Token retrieveCredentialWithIdentifier:@"TwitterToken"];
    [self.twitterClient setAccessToken:twitterToken];
    [self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self.twitterClient getPath:@"users/lookup.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        userLookup = responseObject;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

检测用户是否在页面底部的方法

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tweets count] == (indexPath.row+1)) {
        NSDictionary *totalArray = tweets[indexPath.row];
        NSString *cellID = [totalArray objectForKey:@"id"];
        NSLog(@"%@",cellID);
        [self fetchNextTwitterPageWithID:cellID];
    }
}

【问题讨论】:

    标签: ios objective-c uitableview twitter


    【解决方案1】:

    错误信息很清楚。您正在尝试对不可变数组进行变异。根据堆栈跟踪,错误出现在您调用 addObjectsFromArray:fetchNextTwitterPageWithID: 方法中。

    可疑线路是这一行:

    NSMutableArray *responseArray = (NSMutableArray *)responseObject;
    

    很可能responseObject 不是可变数组而是不可变数组。

    将行改为:

    NSMutableArray *responseArray = [responseObject mutableCopy];
    

    另外,这一行是个问题:

    tweets = [tweets copy];
    

    同样,您需要一个可变副本:

    tweets = [tweets mutableCopy];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      相关资源
      最近更新 更多