使用UITableView发出分页

时间:2014-01-06 01:54:53

标签: ios iphone objective-c uitableview uiscrollview

我试图用Instagram使用UITableView和AFNetworking创建一个无限滚动类型的东西但是当我到达视图的底部时我得到了这个错误:

CRASH :
 -[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object
2014-01-05 20:51:41.627 Floadt[1579:70b] STACK TRACE :
 (
    0   CoreFoundation                      0x02b775e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x027a38b6 objc_exception_throw + 44
    2   CoreFoundation                      0x02b773bb +[NSException raise:format:] + 139
    3   CoreFoundation                      0x02bfe365 -[__NSCFArray insertObject:atIndex:] + 101
    4   CoreFoundation                      0x02b3b2d0 -[NSMutableArray insertObjects:count:atIndex:] + 208
    5   CoreFoundation                      0x02b3af69 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 425
    6   CoreFoundation                      0x02b3ad15 -[NSMutableArray addObjectsFromArray:] + 661
    7   Floadt                              0x0003f5ec __42-[StreamViewController nextInstagramPage:]_block_invoke + 284
    8   Floadt                              0x0001edfb __64-[AFJSONRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke91 + 43
    9   libdispatch.dylib                   0x035247f8 _dispatch_call_block_and_release + 15
    10  libdispatch.dylib                   0x035394b0 _dispatch_client_callout + 14
    11  libdispatch.dylib                   0x0352775e _dispatch_main_queue_callback_4CF + 340
    12  CoreFoundation                      0x02bdca5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
    13  CoreFoundation                      0x02b1d6bb __CFRunLoopRun + 1963
    14  CoreFoundation                      0x02b1cac3 CFRunLoopRunSpecific + 467
    15  CoreFoundation                      0x02b1c8db CFRunLoopRunInMode + 123
    16  GraphicsServices                    0x0453c9e2 GSEventRunModal + 192
    17  GraphicsServices                    0x0453c809 GSEventRun + 104
    18  UIKit                               0x01936d3b UIApplicationMain + 1225
    19  Floadt                              0x0006646d main + 141
    20  libdyld.dylib                       0x037cb70d start + 1
    21  ???                                 0x00000001 0x0 + 1
)

以下是我制作代码的方法,让我感到困惑的是,我的所有数组都是Mutable,但它表明变异方法被发送到不可变对象:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y == roundf(scrollView.contentSize.height-scrollView.frame.size.height)) {
        NSDictionary *page = instagramResponse[@"pagination"];
        NSString *nextPage = page[@"next_url"];

        [[InstagramClient sharedClient] getPath:[NSString stringWithFormat:@"%@",nextPage] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

            instagramResponse = [responseObject mutableCopy];
            [instagramResponse addEntriesFromDictionary:responseObject];
            [instaPics addObjectsFromArray:responseObject[@"data"]];
            [self.tableView reloadData];

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Failure: %@", error);
        }];
    }
}

1 个答案:

答案 0 :(得分:1)

addEntriesFromDictionary之前复制Dictionarylike this

NSDictionary *copyOfDic = [[NSDictionary alloc] initWithDictionary: originalDic copyItems: YES];
if (copyOfDic ) {
    [destination addEntriesFromDictionary: copyOfDic ];
    [copyOfDic release];
}

[instaPics addObjectsFromArray:[responseObject[@"data"]mutable copy]];

您的@property yourarray要么使用NSArray进行初始化,要么使用声明的副本进行初始化。

因为两者都会导致此错误/异常消息,因为支持ivar会指向(不可变的)NSArray。

如果您已将您的属性声明为NSMutableArray类型,请使用strong作为存储修饰符而不是复制。

相关问题