[英]issue adding items to NSArray from other Array
我正在嘗試將Instagram圖片添加到與Twitter Tweets相同的數組中,但是當我加載該應用程序時,這些Tweets僅在第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
中的位置indexpath.row
我認為在主隊列上重新加載數據可以解決此問題。
在fetchInstagramPics和fetchTweets方法中都嘗試此操作:
dispatch_async(dispatch_get_main_queue(),^ {
[self.tableView reloadData]; });
讓我知道它是否有效。
我不知道您到底想實現什么目標,但看來您形成陣列的方式不太正確。
- (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和推文。 獨立使用它們。
- (void)updateArrays { totalFeed = [instaPics arrayByAddingObjectsFromArray:tweets]; // concatinates two arrays. // rmeove this // instaPics = [totalFeed mutableCopy]; // tweets = [totalFeed mutableCopy]; }
您的總供稿包含推文和instagram圖片的備用數據結構。 和instaPics&tweets數組是該數組的冗余副本。
如果要將它們保留在同一單元格中,則創建一個單元格並使用不帶%2條件的totalFeed數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.