ios 获取最后一个cell_关于ios:向UICollectionView的第一个和最后一个单元格添加填充...

本文介绍了如何在iOS中自定义UICollectionViewFlowLayout,以实现水平分页滚动,并为UICollectionView的第一个和最后一个单元格添加填充。内容涉及到计算目标内容偏移、设置contentInset以及处理边缘空白的方法。
摘要由CSDN通过智能技术生成

我子类化UICollectionViewFlowLayout以获取具有分页行为的水平UICollectionView。 只要UICollectionViewCell不是最后一个单元格,它就可以很好地工作。 图片如下。

除了以下内容,我还需要重写UICollectionViewFlowLayout中的内容吗?

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity

{

CGFloat offSetAdjustment = MAXFLOAT;

CGFloat horizontalCenter = (CGFloat) (proposedContentOffset.x + (self.collectionView.bounds.size.width / 2.0));

CGRect targetRect = CGRectMake(proposedContentOffset.x,

0.0,

self.collectionView.bounds.size.width,

self.collectionView.bounds.size.height);

NSArray *array = [self layoutAttributesForElementsInRect:targetRect];

for (UICollectionViewLayoutAttributes *layoutAttributes in array)

{

if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell)

{

CGFloat itemHorizontalCenter = layoutAttributes.center.x;

if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offSetAdjustment))

{

offSetAdjustment = itemHorizontalCenter - horizontalCenter;

}

}

}

CGFloat nextOffset = proposedContentOffset.x + offSetAdjustment;

do {

proposedContentOffset.x = nextOffset;

CGFloat deltaX = proposedContentOffset.x - self.collectionView.contentOffset.x;

CGFloat velX = velocity.x;

if(deltaX == 0.0 || velX == 0 || (velX > 0.0 && deltaX > 0.0) || (velX < 0.0 && deltaX < 0.0))

{

break;

}

if(velocity.x > 0.0)

{

nextOffset += [self snapStep];

}

else if(velocity.x < 0.0)

{

nextOffset -= [self snapStep];

}

} while ([self isValidOffset:nextOffset]);

proposedContentOffset.y = 0.0;

return proposedContentOffset;

}

- (BOOL)isValidOffset:(CGFloat)offset

{

return (offset >= [self minContentOffset] && offset <= [self maxContentOffset]);

}

- (CGFloat)minContentOffset

{

return -self.collectionView.contentInset.left;

}

- (CGFloat)maxContentOffset

{

return [self minContentOffset] + self.collectionView.contentSize.width -      self.itemSize.width;

}

- (CGFloat)snapStep

{

return self.itemSize.width + self.minimumLineSpacing;

}

任何指针/注释将很有用。

您可以使用UICollectionViewFlowLayout的UIEdgeInsets sectionInset属性。 请参阅这篇文章。 另请参阅"使用中的Apple文档"部分插图

设置集合视图的框架时,可以在左右两侧设置与填充相同的空间。

要么

您可以在cellForItemAtIndexPath中放置条件,如果它是第一个单元格或最后一个单元格,则相应地管理填充。而已。

要么

您可以设置collectionView的contentInset属性。

例如,

UICollectionView *cv; // your collectionView

cv.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);

另外,您可以在情节提要中设置UICollectionView contentInset使其起作用。

抱歉,在过去30分钟里,我一直在与节插图一起玩,没想到设置UICollectionView本身的插图。

是的,设置contentInset是适合您的情况的好解决方案。 :)

谢谢,这个答案对我有很大帮助

欢迎您@IvanCantarino! :)

contentInset是有意义的,但是如果将其放入最后一个项目并追加另一个项目,则将其放入cellForItemAtIndexPath效果不佳,因为,如果滚动到末尾并添加了新项目,则通常会为new最终项目调用cellForItemAtIndexPath,但不再要求之前的最终商品。或至少不能保证被调用。

不,您不应该在cellForItemAtIndexPath中设置contentinsett,我在回答中也没有这样说!

您可以通过在Interface Builder中更改插图来实现。它们可以在"大小"检查器中作为"部分插入"找到:

对于Swift 5:

在您的viewDidLoad中,像下面这样设置collectionView的contentInset属性:

self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);

很简单,您可以使用collectionview方法像下面的方法一样设置UIEdgeInsets。

-(UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

return UIEdgeInsetsMake(0,10,0,10); // top, left, bottom, right

}

您可以在此处传递第一个和最后一个单元格的左侧空间和右侧空间的值,也可以通过波纹管方法在两个单元格之间提供最小空间

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

return 5.0;

}

创建UICollectionViewFlowLayout的实例时,我已经设置了插图。因此它无济于事,minimumInteritemSpacing在这里没有用,因为我在进行水平滚动。您必须在这里使用我已经在做的minimumLineSpacing。

接受的解决方案有效,但是如果您具有pageingEnabled,则收集视图分页将被破坏。

对我来说,解决方案是使用:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

return UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值