[英]Custom layout for collection view in swift?
I have 7 cells in one section in a UICollectionView. 我在UICollectionView的一个部分中有7个单元格。 I have searched for hours but all I can find is information about CollectionFlow and Custom layouts with hundreds of cells.
我搜索了几个小时,但我能找到的是有关数百个单元格的CollectionFlow和Custom布局的信息。 All I want is display 7 cells with different identifiers in a collection view using a layout like this image:
我想要的是在集合视图中使用如下图像的布局显示7个具有不同标识符的单元格:
How can I achieve this layout? 我怎样才能实现这种布局?
Follow this URL step by step.... 一步一步地遵循此URL ....
SWIFT : SWIFT:
http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1 http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1
OBJECTIVE C : 目标C:
https://github.com/eoghain/RBCollectionViewBalancedColumnLayout https://github.com/eoghain/RBCollectionViewBalancedColumnLayout
That is my solution. 这是我的解决方案。
class HistoryLayout: UICollectionViewLayout {
var delegate : HistoryLayoutDelegate?
var yOffSet : CGFloat = 0
var xOffSetLand : CGFloat = 0
var xOffSetPort : CGFloat = 0
var portraitElements : [Int] = []
var portraintItemPositionIndex = PortraintItemPositionIndex.Left
private var cache = [CGRect]()
var contentHeight: CGFloat = 0.0
var contentWidth: CGFloat {
let insets = collectionView!.contentInset
return CGRectGetWidth(collectionView!.bounds) - (insets.left + insets.right)
}
override func collectionViewContentSize() -> CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for item in 0 ..< collectionView!.numberOfItemsInSection(0) {
let indexPath = NSIndexPath(forItem: item, inSection: 0)
let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attributes.frame = cache[item]
if CGRectIntersectsRect(attributes.frame, rect) {
layoutAttributes.append(attributes)
}
}
//send index list of elements with portraint position
if let _ = delegate {
delegate?.elementsWithVerticalPosition(self.portraitElements)
}
return layoutAttributes
}
override func prepareLayout() {
if cache.isEmpty {
var gropCount = Int(collectionView!.numberOfItemsInSection(0) / 3)
if collectionView!.numberOfItemsInSection(0) % 3 != 0 {
gropCount++
}
for index in 1...gropCount {
self.improGroupByItem(index)
contentHeight = ((contentWidth / 3) * 2) * CGFloat(index)
}
}
}
private func improGroupByItem(index : Int){
if index % 2 == 0 {
xOffSetLand = contentWidth / 3
xOffSetPort = 0
self.portraintItemPositionIndex = .Right
}else{
xOffSetLand = 0
xOffSetPort = (contentWidth / 3) * 2
self.portraintItemPositionIndex = .Left
}
if index > 1 {
self.yOffSet += (contentWidth / 3) * 2
}
addItemToStack(index)
}
func addItemToStack(gIndex : Int){
let columnWidth = self.contentWidth / 3
var frame : CGRect
var dublicate = 0
for index in 1...3 {
if index % portraintItemPositionIndex.rawValue == 0 {
let width = columnWidth * 1
let height = columnWidth * 2
frame = CGRect(x: self.xOffSetPort, y: yOffSet, width: width, height: height)
let indexOfPortElement = ((gIndex - 1)*3) + index - 1
portraitElements.append(indexOfPortElement)
}else{
let width = columnWidth * 2
let height = columnWidth * 1
frame = CGRect(x: self.xOffSetLand, y: yOffSet + (height * CGFloat(dublicate)), width: width, height: height)
dublicate++
}
cache.append(frame)
}
}
}
Two solutions for you I think: 我认为有两种解决方案:
use a library: https://github.com/bryceredd/RFQuiltLayout 使用库: https : //github.com/bryceredd/RFQuiltLayout
use FlowLayout: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html 使用FlowLayout: https : //developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html
I think you have all you need! 我想你有你需要的一切! ;)
;)
It is possible with the FlowLayout, but you have to customize the FlowLayout Attributes. 可以使用FlowLayout,但您必须自定义FlowLayout属性。
For different cell sizes use this method - 对于不同的单元尺寸使用此方法 -
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.