将数据分配给我的UICollectionViewCell
时,会添加数据但会在它们之间留下一个空格。
显示UICollectionView
时,我希望结果为:
[1] [2] [3] [4]等等
但我得到了这个:
[1] [] [2] [] [3] [] [4]
来自viewDidLoad
的代码:
- (void) viewDidLoad {
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 70) collectionViewLayout:layout];
_collectionView.userInteractionEnabled = YES;
_collectionView.alwaysBounceHorizontal = YES;
_collectionView.multipleTouchEnabled = NO;
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView setPagingEnabled:NO];
[_collectionView setScrollEnabled:YES];
[_collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"CustomCell"];
}
来自cellForItemAtIndexPath
的代码:
- (CustomCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:@"%d", indexPath.row]; //testing row
return cell;
}
我的CustomCell.m
:
- (id)initWithFrame:(CGRect)aRect
{
self = [super initWithFrame:aRect];
{
titleLabel = [[UILabel alloc] initWithFrame:self.frame];
[self addSubview:titleLabel];
}
return self;
}
答案 0 :(得分:0)
您应该尝试实现其中一种(如果不是全部)方法:
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
-(CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
-(CGFloat) collectionView:(UICollectionView *) collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
UICollectionViewDelegateFlowLayout 负责UICollectionView中单元格的大小和间距。了解更多here。
然而,如果你想完全解决并找出哪个是主要问题,你应该按Xcode底部栏上的第7个按钮调试你的视图(我有7.2版本)。这是一个快照:
这就是它应该如何调试:
答案 1 :(得分:0)
我已经记录了CustomCell
的子视图,我认为在将框架分配给UILabel
时会出现错误。
记录titleLabel = [[UILabel alloc] initWithFrame:self.frame]
:
App(
"<UIView: 0x7b74cb00; frame = (0 0; 50 70); gestureRecognizers = <NSArray: 0x7b74d0b0>; layer = <CALayer: 0x7b74cbc0>>",
"<UILabel: 0x7b74d0e0; frame = (0 0; 50 70); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7b74d1f0>>"
)
App (
"<UIView: 0x7c954cf0; frame = (0 0; 50 70); gestureRecognizers = <NSArray: 0x7c919a10>; layer = <CALayer: 0x7c9f30e0>>",
"<UILabel: 0x7c985bc0; frame = (55 0; 50 70); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7c952020>>"
)
&#13;
正如您所见,UILabel
的框架被推向右侧。
记录titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 70)]
:
App (
"<UIView: 0x7c2a1f00; frame = (0 0; 50 70); gestureRecognizers = <NSArray: 0x7c2924f0>; layer = <CALayer: 0x7c2a4970>>",
"<UILabel: 0x7c2a3c60; frame = (0 0; 50 70); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7c292520>>"
)
App (
"<UIView: 0x7c2a3f30; frame = (0 0; 50 70); gestureRecognizers = <NSArray: 0x7c2a5ce0>; layer = <CALayer: 0x7c2a3ff0>>",
"<UILabel: 0x7c2a5d10; frame = (0 0; 50 70); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7c2a5e20>>"
)
&#13;
现在,UILabel
的框架与UICollectionViewCell
的框架分别与UIView
完全相同。