ios - 将项目从其他数组添加到 NSArray 时出现问题

标签 ios iphone objective-c arrays twitter

我正在尝试将 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

中的位置

Screenshot of the App

最佳答案

我认为在主队列上重新加载数据可以解决这个问题。

在 fetchInstagramPics 和 fetchTweets 方法中试试这个:

dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData]; });

让我知道它是否有效。

关于ios - 将项目从其他数组添加到 NSArray 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20158337/

相关文章:

与从 IDE 构建/运行相比,iOS 14 不允许 Flutter 应用程序(仍处于开发阶段)从主屏幕启动

ios - 是否可以通过编程将音频输出重定向到 iOS 中的电话扬声器或耳机

objective-c - 如何使用 Swift 和 Objective-C 创建 XML 文件?

iphone - View 之间的基本导航不起作用

ios - XCode UITests 替代.StaticText

ios - Swift:滚动时标签和图像的 UICollectionView 单元格重复

ios - 异步下载 - 右模式

iphone - 如何获取 iPhone 的钛型号名称?

iphone - 我可以更改uitableview的tableHeaderView的高度吗?

ios - 从 sizewithFont :constrainedToSize:lineBreakMode: for iOS 7 not behaving as expected 转换