ios 获取最后一个cell_关于ios:向UICollectionView的第一个和最后一个单元格添加填充...
我子類化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)
}
總結
以上是生活随笔為你收集整理的ios 获取最后一个cell_关于ios:向UICollectionView的第一个和最后一个单元格添加填充...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iview的表格自定义_Vue中使用iv
- 下一篇: retrofit2使用详解_秒懂Retr