UiCollectionView从给定索引开始冻结

时间:2015-04-27 08:30:57

标签: ios scroll xcode6 uicollectionview

根据我在Stackoverflow中找到的一些答案,我应该使用这段代码:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    [self.collectionView scrollToItemAtIndexPath:selectedRow atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

但滚动冻结,所以我必须这样设置:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.title = [NSString stringWithString:barTitle];

    self.automaticallyAdjustsScrollViewInsets = NO;

    //Download JSON
    NSError *error=nil;

    NSURL *url = [NSURL URLWithString:urlToFollow];
    data = [NSMutableData dataWithContentsOfURL: url options:NSDataReadingMappedIfSafe error:&error];
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

    NSArray *response = [dictionary objectForKey:@"items"];
    articlesArray = [[NSArray alloc] initWithArray:response];

    //sizes
    CGFloat window_height = ([self window_height]-70.0);
    CGFloat window_width  = [self window_width];
    CGRect frame = CGRectMake((window_width/2)-15.0f , window_height, 30.0f , 15.0f);

    self.pageControl = [[UIPageControl alloc]initWithFrame:frame];
    UICollectionViewFlowLayout *layout = (id) self.collectionView.collectionViewLayout;
    layout.itemSize = self.collectionView.frame.size;

    // Add a target that will be invoked when the page control is changed by tapping on it
    [self.pageControl
     addTarget:self
     action:@selector(pageControlChanged:)
     forControlEvents:UIControlEventValueChanged
     ];

    // Set the number of pages to the number of pages in the paged interface
    // and let the height flex so that it sits nicely in its frame
    if (articlesArray.count >8) {
        self.pageControl.numberOfPages = 10;
    }else if (articlesArray.count <8){
        self.pageControl.numberOfPages =[articlesArray count];
    }
    self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.pageControl];

- (void)viewDidAppear:(BOOL)animated  {

    [super viewDidAppear:animated];

    [self.collectionView scrollToItemAtIndexPath:selectedRow atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];

}
#pragma mark uicollectionview

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

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [articlesArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LOArcticlesCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    [cell.imageView setImageWithURL:[NSURL URLWithString:[[articlesArray objectAtIndex:indexPath.row]objectForKey:@"photoUrl"]]placeholderImage:[UIImage imageNamed:@"simboloLakari29.png"]];

    index= indexPath.row;

    cell.lblMake.text = [NSString stringWithString:[[articlesArray objectAtIndex:indexPath.row]objectForKey:@"marca"]];
    cell.lblMake.lineBreakMode = NSLineBreakByWordWrapping;
    cell.lblModel.text = [NSString stringWithString:[[articlesArray objectAtIndex:indexPath.row]objectForKey:@"modelo"]];
    cell.lblModel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.lblPrice.text = [NSString stringWithString:[[articlesArray objectAtIndex:indexPath.row]objectForKey:@"precio"]];
    cell.lblOrder.text = [NSString stringWithFormat:@"%ld de %ld", (unsigned long)indexPath.row+1, (unsigned long)articlesArray.count];

    return cell;
}

#pragma mark page control

- (void)pageControlChanged:(id)sender
{
    UIPageControl *pageControl = sender;
    CGFloat pageWidth = self.collectionView.frame.size.width;
    CGPoint scrollTo = CGPointMake(pageWidth * pageControl.currentPage, 0);
    [self.collectionView setContentOffset:scrollTo animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = self.collectionView.frame.size.width;
    self.pageControl.currentPage = self.collectionView.contentOffset.x / pageWidth;
}
- (CGFloat) window_height   {
    return [UIScreen mainScreen].applicationFrame.size.height;
}

- (CGFloat) window_width   {
    return [UIScreen mainScreen].applicationFrame.size.width;
}

但是CollectionView从index = 0变为所需的索引并且非常烦人。

有什么建议吗?

0 个答案:

没有答案