这是我关于stackoverflow的第一个问题,所以我希望我的问题能够清楚。
所以我有一个UIViewController(A),我在顶部添加了另一个UIViewController(B)视图的视图。
UICollectionView位于B上并配置了Xib。 UICollectionView的代表& dataSource链接到视图控制器,我在水平滚动方向使用标准布局。单元格大小在xib中配置:288x80。 (collectionView大小为320x80)。
我的问题是在collectionView:cellForItemAtIndexPath:
中返回一个红色单元格但未显示。我真的不知道为什么没有表现出来,而且我很头疼:)。
提前感谢您的帮助!
- (id)init
{
_arroundPanel = /*init*/;
}
- (void)viewDidLoad
{
[_arroundPanel showPanelOnViewController:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
/*...*/
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
}
- (void)showPanelOnViewController:(UIViewController *)controller
{
_viewController = controller;
[self view];
_collectionViewHeight.constant = 0;
[self.view layoutIfNeeded];
[self startStateWithAnimation:NO];
[_viewController.view addSubview:self.view];
_defaultTop = (_viewController.navigationController) ? 64.0 : 0.0;
[self.view mas_makeConstraints:^(MASConstraintMaker *make) {
_viewTopConstraint = make.top.equalTo(_viewController.view).with.offset(_defaultTop);
make.left.equalTo(_viewController.view);
make.right.equalTo(_viewController.view);
}];
_tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startSearch)];
[self.view addGestureRecognizer:_tap];
if(_delegate) [_delegate panelChangeHeight:[self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height];
}
- (void)startSearch
{
_tap.enabled = NO;
/*...*/
[self loadSellers];
}
- (void)loadSellers
{
if(_isSearching) return;
_isSearching = NO;
[LCSeller sellerNearbyUsingBlock:^(NSArray *sellers) {
_sellers = sellers;
[self sellersLoaded];
} failureBlock:^(NSError *error) {
/*...*/
}];
}
- (void)sellersLoaded
{
CGFloat heightBefore = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
if((_sellers == nil || _sellers.count == 0) && _collectionViewContainerViewHeight.constant > 0)
{
_collectionViewContainerViewHeight.constant = 0;
if(_delegate) [_delegate panelChangeHeight:[self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height - heightBefore];
[UIView animateWithDuration:0.3 animations:^{
[_viewController.view layoutIfNeeded];
} completion:^(BOOL finished) {
[self.collectionView reloadData];
}];
}
else if (_sellers.count > 0)
{
if(_collectionViewContainerViewHeight.constant == 0)
{
_collectionViewContainerViewHeight.constant = _collectionView.bounds.size.height;
if(_delegate) [_delegate panelChangeHeight:[self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height - heightBefore];
[UIView animateWithDuration:0.3 animations:^{
[_viewController.view layoutIfNeeded];
} completion:^(BOOL finished) {
[self.collectionView reloadData];
}];
}
else
{
[self.collectionView reloadData];
}
}
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return _sellers.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[_collectionView deselectItemAtIndexPath:indexPath animated:NO];
if(_delegate) [_delegate panelSelectSeller:_sellers[indexPath.row]];
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(_collectionView.bounds.size.width - 32, _collectionView.bounds.size.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsZero;
}
在return cell;
collectionView:cellForItemAtIndexPath:
的断点处
(lldb) po cell
<UICollectionViewCell: 0xdde1df0; frame = (0 0; 288 80); layer = <CALayer: 0xdde1f00>>
我终于找到了导致细胞不在collectionView中显示的原因。
当调用showPanelOnViewController:
时,我将集合视图的高度约束设置为0(因为我不想在加载数据之前显示它)所以如果我删除这些行,它会工作:
_collectionViewHeight.constant = 0;
[self.view layoutIfNeeded];
这是一个奇怪的事情,因为我只在集合视图的高度是好的时才调用reloadData ... 我还将collectionView放在一个带有clipToBounds的UIView(一个容器)中,并且改变了这个容器的高度而不是collectionView,但是bug仍在这里。
如果我找到了保留集合视图高度动画的解决方案,我会通知您。
答案 0 :(得分:0)
Interface Builder文件是否使用Prototype Cells?
我要做的是在xib的集合视图中放入1个UICollectionViewCell,并从viewDidLoad中删除registerClass:
。
同样直接访问另一个视图控制器的视图并将其添加为子视图并不是通常的做事方式。您应该1)让集合视图的视图控制器控制整个“屏幕”或2)使UIViewController(B)成为管理集合视图的UIView(或NSObject)的子类,并允许它作为<添加到视图层次结构中em>只是一个视图而不是视图控制器。请参阅View Programming Guide
答案 1 :(得分:0)
在这个错误2天后,我找到了解决方案!
在编辑我的问题时,我发现了错误的位置。
因此,要让我的集合视图正常工作,我现在将集合视图的高度设置为viewWillAppear:
中的0而不是showPanelOnViewController:
。
我认为在viewWillAppear之前,集合视图正在计算大小...
的东西