gpt4 book ai didi

ios - 在运行时向 UITableView 添加更多项目

转载 作者:行者123 更新时间:2023-11-29 01:58:30 25 4
gpt4 key购买 nike

我当前正在尝试分页 Twitter Feed,但是当我尝试添加分页到现有 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 底部时将分页项目添加到原始项目下方。

检索分页推文的方法

-(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);
}];
}

推特用户查询方法

- (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];
}
}

最佳答案

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

可疑行是这一行:

NSMutableArray *responseArray = (NSMutableArray *)responseObject;

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

将行更改为:

NSMutableArray *responseArray = [responseObject mutableCopy];

此外,这一行也是一个问题:

tweets = [tweets copy];

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

tweets = [tweets mutableCopy];

关于ios - 在运行时向 UITableView 添加更多项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com