我正在尝试将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
答案 0 :(得分:1)
我认为在主队列上重新加载数据可以解决问题。
在fetchInstagramPics和fetchTweets方法中尝试:
dispatch_async(dispatch_get_main_queue(),^ {
[self.tableView reloadData]; });
告诉我它是否有效。
答案 1 :(得分: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,就像我已附加图像一样 只是不要总结instaPics&有totalFeed的推文。独立使用它们。
- (void)updateArrays { totalFeed = [instaPics arrayByAddingObjectsFromArray:tweets]; // concatinates two arrays. // rmeove this // instaPics = [totalFeed mutableCopy]; // tweets = [totalFeed mutableCopy]; }
您的总Feed包含推文和Instagram图片的备用数据结构。 和instaPics& tweets数组是它的冗余副本。
如果你想将它们保存在同一个单元格中,那么创建一次单元格并使用不带%2条件的totalFeed数组。