2013-07-26 3 views
4

이제는 Instagram의 이미지를 새 셀을 추가하는 대신 화면 맨 아래로 옮겨 놓습니다. 이전 셀을 교체하는 대신 새로운 셀을 추가하는 방법은 무엇입니까?더 많은 셀 추가 UICollectionView

다음
@interface StreamViewController() <UITextFieldDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> 
@property (nonatomic, strong) NSMutableDictionary *timelineResponse; 
@property (nonatomic, strong) CredentialStore *credentialStore; 
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView; 

@end 

@implementation StreamViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self refreshInstagram]; 

    //Refresh 
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; 
    [refreshControl addTarget:self action:@selector(startRefresh:) 
      forControlEvents:UIControlEventValueChanged]; 
    [self.collectionView addSubview:refreshControl]; 

    //Instigate Navigation Bar Buttons 
    UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    [barButton setTitle:@"" forState:UIControlStateNormal]; 
    [barButton setBackgroundImage:[UIImage imageNamed:@"barButton.png"] forState:UIControlStateNormal]; 
    [barButton setBackgroundImage:[UIImage imageNamed:@"barButton_s.png"] forState:UIControlStateHighlighted]; 
    [barButton addTarget:self action:@selector(didTapBarButton:) forControlEvents:UIControlEventTouchUpInside]; 
    barButton.frame = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f); 
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton]; 

    self.navBar.leftBarButtonItem = barButtonItem; 

    UIButton *postButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    [postButton setTitle:@"" forState:UIControlStateNormal]; 
    [postButton setBackgroundImage:[UIImage imageNamed:@"pen_usIMG.png"] forState:UIControlStateNormal]; 
    [postButton setBackgroundImage:[UIImage imageNamed:@"pen_sIMG.png"] forState:UIControlStateHighlighted]; 
    [postButton addTarget:self action:@selector(didTapPostButton:) forControlEvents:UIControlEventTouchUpInside]; 
    postButton.frame = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f); 
    UIBarButtonItem *postButtonItem = [[UIBarButtonItem alloc] initWithCustomView:postButton]; 

    self.navBar.rightBarButtonItem = postButtonItem; 

    //Reload by default 
    [self.collectionView reloadData]; 

} 


//Global refresh Instagram Method 
- (void)refreshInstagram { 

    [[InstagramClient sharedClient] getPath:@"users/self/feed" 
           parameters:nil 
            success:^(AFHTTPRequestOperation *operation, id responseObject) { 
             NSLog(@"Response: %@", responseObject); 
             self.timelineResponse = responseObject; 
             [self.collectionView reloadData]; 

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
             NSLog(@"Failure: %@", error); 
            }]; 

} 

- (void)nextInstagramPage:(NSIndexPath *)indexPath{ 
    NSDictionary *page = self.timelineResponse[@"pagination"]; 
    NSString *nextPage = page[@"next_url"]; 

    [[InstagramClient sharedClient] getPath:[NSString stringWithFormat:@"%@",nextPage] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     [self.collectionView cellForItemAtIndexPath:indexPath]; 
     self.timelineResponse = [responseObject mutableCopy]; 
     [self.timelineResponse addEntriesFromDictionary:responseObject]; 
     [self.collectionView reloadData]; 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Failure: %@", error); 
    }]; 



} 
- (NSMutableArray *)entries { 
    return self.timelineResponse[@"data"]; 
} 

- (NSArray *)pages { 
    return self.timelineResponse[@"pagination"]; 
} 


- (NSURL *)imageUrlForEntryAtIndexPath:(NSIndexPath *)indexPath { 
    NSDictionary *entry = [self entries][indexPath.row]; 
    NSString *imageUrlString = entry[@"images"][@"standard_resolution"][@"url"]; 
    return [NSURL URLWithString:imageUrlString]; 
} 

#pragma mark - UICollectionViewDelegate 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    //int y = arc4random() % 200+50; 


    return CGSizeMake(150, 150); 


} 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self title:@"Item Tapped!" message:@"Thank God its working"]; 
    [modal show]; 
} 


- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (scrollView.contentOffset.y == roundf(scrollView.contentSize.height-scrollView.frame.size.height)) { 
     NSLog(@"we are at the endddd"); 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

     [self.collectionView performBatchUpdates:^{ 
      [self nextInstagramPage:indexPath]; 

     } completion:^(BOOL finished) { 


     } 
     ]; 
    } 
} 

#pragma mark - UICollectionViewDataSource 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 

    return [[self entries] count]; 

} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

    if (indexPath.item == ([[self entries] count] - 1)) { 
     // download more data and add it to the 'entries' array 
     [self refreshInstagram]; 


    } 

    ImageCell *cell = (ImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" 
                      forIndexPath:indexPath]; 
    NSURL *url = [self imageUrlForEntryAtIndexPath:indexPath]; 
    NSLog(@"%@", url); 
    [cell.imageView setImageWithURL:url]; 
    cell.backgroundColor = [UIColor whiteColor]; 

    return cell; 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
    return 1; 
} 

#pragma mark - NavigationBarButtons 

- (void)didTapBarButton:(id)sender { 

    [self.sidePanelController showLeftPanelAnimated:YES]; 

} 

- (void)startRefresh:(UIRefreshControl *)sender { 
    [self refreshInstagram]; 
    [sender endRefreshing]; 

} 

-(void)didTapPostButton:(id)sender { 


} 

@end 

새로운 세포를 추가하는 방법입니다 : 여기

이미지의 다음 페이지 검색 내 총 implemantation 파일입니다

-(void)addNewCells { 
    [self.collectionView performBatchUpdates:^{ 
     int resultsSize = [[self entries]count]; 
     [self.results addObjectsFromArray:newData]; 
     NSMutableArray *arrayWithIndexPaths = [NSMutableArray array]; 
     for (int i = resultsSize; i < resultsSize + newData.count; i++) 
      [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
    } 
    [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths]; 
    } 
            completion:nil]; 
} 
+0

'collectionView : (UICollectionView *) view numberOfItemsInSection : (NSInteger) section'의 반환 값을 업데이트 해 보았습니까? (상응하게) 당신이 당신의 심상을 지키는 더 많은 심상을 배열 (또는 어떤 콘테이너)에 추가 하는가? –

+0

배열의 이미지 양을 만들었습니다. – Prad

+1

사용자가 맨 아래로 스크롤하면 더 많은 이미지를 가져 와서 배열에 추가하여 (이제는 더 오래되고 더 새로운 이미지를 포함하도록), 제가 아는 한, 'reloadData'를 호출하면 (numberOfItemsInSection은 이제 [yourArray count]를 반환합니다.) 원하는대로 처리해야합니다. –

답변

2

당신은 배열의 서비스에서 반환되는 사진을 유지해야합니다. 사전은 대답을 추가하기 때문에 사전에서 응답이 일 때 기존 키/값 쌍을 대체하기 때문에 Mundi의 대답처럼 작동하지 않습니다. 그래서 깜박이는 것을 보았습니다.

보기 컨트롤러에 NSMutableArray 속성을 추가하고 데이터의 항목을 추가하십시오. 당신의 entries 방법

[self.photosArray addObjectsFromArray:responseObject[@"data"]]; 

그리고 사용자 self.photosArray.

+0

모든 도움에 감사드립니다 – Prad

2

당신은 단순히 데이터를 증가 정렬. timelineResponseNSMutableArray

[self.timelineResponse addObjectsFromArray : responseObject]로 설정하십시오.

[self.timelineResponse addEntriesFromDictionary:responseObject]; 
[self.collectionView reloadData]; 
+0

현재 timelineResponse는 NSDictionary이며 nsmutable 배열로 변경하지 않고도 동일한 작업을 수행 할 수 있습니다. 위의 .m 파일을 참조 용으로 추가했습니다. – Prad

+0

글쎄, 당신은 아이디어를 얻었습니다. – Mundi

+0

기다려주세요. 사전에 배열을 바꾸지 않습니까? – Prad