Ответ 1
Отвечая на некоторые ваши другие вопросы о UICollectionView, я создал этот демонстрационный проект, и он прокручивается просто отлично, поэтому я не знаю, в чем проблема. Единственное, что мне нужно было сделать, чтобы последняя строка была полностью видимой, заключалась в том, чтобы увеличить размер нижней вставки до 90. Я также добавил метод завершения для executeBatchUpdates: complete: для автоматического прокрутки, чтобы был видимым последний добавленный элемент ( обратите внимание, что я добавляю дополнительные элементы с помощью функции performSelector; withObject: afterDelay:). Мои изображения 48x48, и я просто установил свой просмотр коллекции в рамки моего представления контроллера. Вот код, чтобы вы могли сравнить его с тем, что у вас есть:
@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
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);
}