UICollectionView with FlowLayout not scrolling when setting section insets

后端 未结 3 1746
萌比男神i
萌比男神i 2021-02-03 11:22

I am using a UICollectionView programmatically.

I\'ve set its frame as follows:

UICollectionView *collectionView = [[UICollectionView alloc] initWithFram         


        
3条回答
  •  我在风中等你
    2021-02-03 12:07

    In answering some of your other questions about UICollectionView, I created this demo project, and it scrolls just fine, so I don't know what problem you're having. The only thing I needed to do to make the last row wholly visible was to increase the size of the bottom inset to 90. I also added a completion method to the performBatchUpdates:completion: to automatically scroll so that the last added item is visible (notice that I add some extra items by using performSelector;withObject:afterDelay:). My images are 48x48, and I just set my collection view to the bounds of my controller's view. Here is the code so you can compare it to what you have:

    @interface ViewController ()  {
    NSMutableArray *newData;
    }
     @property (nonatomic, retain) UICollectionView *collectionView;
     @property (nonatomic, retain) NSMutableArray *results;
     @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        self.results = [NSMutableArray array];
        int i = 11;
        while (i<91) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"New_PICT00%d.jpg",i]];
            [self.results addObject:image];
            i++;
        }
    
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //flowLayout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.view.backgroundColor = [UIColor blackColor];
        [self.view addSubview:self.collectionView];
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
        //[self.collectionView registerClass:[RDCell class] forCellWithReuseIdentifier:@"myCell"];
        [self.collectionView reloadData];
    
        [self performSelector:@selector(addNewImages:) withObject:nil afterDelay:3];
    }
    
    -(void)addNewImages:(id)sender {
        newData = [NSMutableArray array];
        int i = 102;
        while (i<111) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"New_PICT0%d.jpg",i]];
            [newData addObject:image];
            i++;
        }
        [self addNewCells];
    }
    
    -(void)addNewCells {
        NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
        [self.collectionView performBatchUpdates:^{
            int resultsSize = [self.results count];
            [self.results addObjectsFromArray:newData];
            for (int i = resultsSize; i < resultsSize + newData.count; i++)
                [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
            [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
        }
        completion: ^(BOOL finished){
            [self.collectionView scrollToItemAtIndexPath:arrayWithIndexPaths.lastObject atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
        }];
    }
    
    
    - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
        return [self.results count];
    }
    
    - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
        return 1;
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
        //cell.iv.image = [self.results objectAtIndex:indexPath.row];
        cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
        return cell;
    }
    
    
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        UIImage *image = [self.results objectAtIndex:indexPath.row];
        return CGSizeMake(image.size.width, image.size.height);
    }
    
    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
        return UIEdgeInsetsMake(10, 10, 90, 10);
    }
    
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"Selected Image is Item %d",indexPath.row);
    }
    

提交回复
热议问题