我正在尝试将 Instagram 图片添加到与 Twitter 推文相同的数组中,但当我加载应用程序时,推文仅在第 8 行之后显示。:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return totalFeed.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *created = [tweet objectForKey:@"created_at"];
NSLog(@"%@", created);
static NSString *CellIdentifier = @"TweetCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *text = [tweet objectForKey:@"text"];
NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];
cell.textLabel.text = text;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];
return cell;
if (name == NULL) {
NSLog(@"Tweet Doesent Exist");
}
}else {
static NSString *CellIdentifier = @"InstagramCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *entry = instaPics[indexPath.row];
NSString *imageUrlString = entry[@"images"][@"low_resolution"][@"url"];
NSURL *url = [NSURL URLWithString:imageUrlString];
[cell.imageView setImageWithURL:url];
return cell;
}
}
- (void)fetchTweets {
self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/"] key:@"4oFCF0AjP4PQDUaCh5RQ" secret:@"NxAihESVsdUXSUxtHrml2VBHA0xKofYKmmGS01KaSs"];
[self.twitterClient authorizeUsingOAuthWithRequestTokenPath:@"/oauth/request_token" userAuthorizationPath:@"/oauth/authorize" callbackURL:[NSURL URLWithString:@"floadt://success"] accessTokenPath:@"/oauth/access_token" accessMethod:@"POST" scope:nil success:^(AFOAuth1Token *accessToken, id responseObject) {
[self.twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self.twitterClient getPath:@"statuses/home_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *responseArray = (NSArray *)responseObject;
[responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"Success: %@", obj);
tweets = responseArray;
[self updateArrays];
[self.tableView reloadData];
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
} failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
//[[TwitterClient sharedClient] getTimeline];
}
- (void)fetchInstagramPics {
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
BOOL *status = [user boolForKey:@"isInstagramLoggedIn"];
if (status) {
[[InstagramClient sharedClient] getPath:@"users/self/feed"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"Response: %@", responseObject);
self.timelineResponse = responseObject;
[self updateArrays];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", error);
}];
}
}
- (void)updateArrays {
instaPics = self.timelineResponse[@"data"];
totalFeed = [instaPics arrayByAddingObjectsFromArray:tweets];
instaPics = [totalFeed mutableCopy];
tweets = [totalFeed mutableCopy];
}
- (void)fetchTimeline {
[self fetchInstagramPics];
[self fetchTweets];
[self updateArrays];
[self.tableView reloadData];
}
问题是这些帖子远远低于它们应该在 indexpath.row
最佳答案
我认为在主队列上重新加载数据可以解决这个问题。
在 fetchInstagramPics 和 fetchTweets 方法中试试这个:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData]; });
让我知道它是否有效。
关于ios - 将项目从其他数组添加到 NSArray 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20158337/