UICollectionView 没有滚动到位置

UICollectionView is not scrolling to position

我在静态 UITableView 中有一个 UICollectionView。我试图让 collectionView 滚动到 viewDidLoad 中的 indexPath。它不会滚动到启动时的位置,当我 select 一个 indexPath 它滚动到那个位置。我做了一个产生问题的干净项目。

我认为最简单的方法是不让您尝试重现问题,而是直接上传它。所以这里是:Project with problem

只有两个小问题:

1.The selectedDate 与您的 dates 数组不匹配。比较下图中的两个日期:

正如@Omar Al-Shammary 所说,

2.You 应该在 viewDidAppear 中更新 UI。

解决方法:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    [self setSelectedDate:[calendar dateFromComponents:components]];
}