2013-11-23 31 views
0

我想在Instagram的圖片添加到同一陣列作爲Twitter的鳴叫,但是當我加載應用程序的tweet只排8:問題從其他陣列添加項目的NSArray

- (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

回答

1

我想重裝上主隊列可以解決這個問題的數據。

在兩個fetchInstagramPics和fetchTweets方法嘗試這種情況:

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

並讓我知道它是否有效。

+0

如果您發現該回答有用,請勾選它旁邊的複選標記。 –

0

我不知道你到底想要做什麼,但似乎是你正在形成你的陣列的方式是不正確的。

 
- (void)updateArrays { 

    totalFeed = [instaPics arrayByAddingObjectsFromArray:tweets]; // concatinates two arrays. 
    // NLog your total feed and see the behaviour 
    instaPics = [totalFeed mutableCopy]; 
    tweets = [totalFeed mutableCopy]; 
} 

如果你想獲得的鳴叫和Instagram在替代的小區一樣。我已在圖像 只是不totalFeed總結instaPics &鳴叫。獨立使用它們。 Alternate tweets & instagram pics

 
- (void)updateArrays { 

    totalFeed = [instaPics arrayByAddingObjectsFromArray:tweets]; // concatinates two arrays. 

// rmeove this 

// instaPics = [totalFeed mutableCopy]; 
// tweets = [totalFeed mutableCopy]; 
} 

你的總進料包含用於鳴叫和Instagram圖片替代數據結構。 和instaPics & tweets數組是其冗餘副本。

如果你想讓它們保持在同一個單元格中,那麼創建一次單元格並使用沒有%2條件的totalFeed數組。