CollectionView应滚动到最后一个单元格

时间:2016-10-12 07:31:41

标签: objective-c uicollectionview

我正在使用集合视图来显示我的app的拍摄图像。我希望一旦拍摄的图像集合视图应该自动移动到我的集合视图中的最后一个对象。我的集合水平滚动 请帮我这样做。

我在谷歌搜索过,我找到了i have applied this code in my project

我已在viewwillappear的项目中应用此代码,它显示错误, 请帮我这样做

click here to see我正在拍照并在下面显示,好吧,在这张图片中它有3张照片,如果我再拍一张照片第4张照片将在收藏视图中,如果用户想要第4张图片就意味着,用户想手动滚动,可以看到它......

这是我的代码:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [session startRunning];
    NSInteger section = [self numberOfSectionsInCollectionView:self.collection_View] - 1;
    NSInteger item = [self collectionView:self.collection_View numberOfItemsInSection:section] - 1;
    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
}

我想自动滚动到水平的最后一张照片 在此先感谢!!!

2 个答案:

答案 0 :(得分:2)

let pageTitle = "<p style='color:red;'><b>THIS IS TITLE</b></p>" 方法代码中添加以下行:

viewWillAppear

使用此:

[collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];

答案 1 :(得分:0)

这是已接受答案的Swift 4.2版本

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let section = numberOfSections(in: collectionView) - 1
    let item = collectionView.numberOfItems(inSection: section) - 1
    let lastIndexPath = IndexPath(item: item, section: section)
    collectionView.scrollToItem(at: lastIndexPath, at: .bottom, animated: true)
}
相关问题