Last active
November 1, 2016 18:26
-
-
Save paulmars/66fd20a449032fc9de53cb203f4e2eb1 to your computer and use it in GitHub Desktop.
Basic List - UICollectionView. Two labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ListViewController.swift | |
// | |
// Created by Paul McKellar on 11/1/16. | |
// Copyright © 2016 Paul McKellar. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class BlogViewCell: UICollectionViewCell { | |
var titleLabel: UILabel! | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.titleLabel = UILabel() | |
self.titleLabel.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height) | |
self.titleLabel.tintColor = UIColor.black | |
self.addSubview(titleLabel) | |
return; | |
} | |
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented"); } | |
} | |
class ListLayoutAttributes: UICollectionViewLayoutAttributes { | |
var height: CGFloat = 0.0 | |
} | |
class ListOffsetLayout: UICollectionViewLayout { | |
var delegate: BlogsListViewController! | |
private var items = [ListLayoutAttributes]() | |
private var contentHeight:CGFloat = 0.0 | |
private var contentWidth: CGFloat { | |
let insets = collectionView!.contentInset | |
return collectionView!.bounds.width - (insets.left + insets.right) | |
} | |
override class var layoutAttributesClass: AnyClass { | |
return ListLayoutAttributes.self | |
} | |
override func prepare() { | |
for itemNumber in 0 ..< collectionView!.numberOfItems(inSection: 0) { | |
let indexPath = IndexPath(row: itemNumber, section: 0) | |
let height = delegate.heightForCell(indexPath) | |
let attributes = ListLayoutAttributes(forCellWith: indexPath as IndexPath) | |
attributes.height = height | |
let frame = CGRect(x: 0, y: height * CGFloat(itemNumber), width: contentWidth, height: height) | |
attributes.frame = frame | |
items.append(attributes) | |
contentHeight = contentHeight + height | |
} | |
} | |
override var collectionViewContentSize: CGSize { | |
return CGSize(width: contentWidth, height: contentHeight) | |
} | |
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { | |
return items[indexPath.row] | |
} | |
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { | |
var layoutAttributes = [ListLayoutAttributes]() | |
for attributes in items { | |
if attributes.frame.intersects(rect) { | |
layoutAttributes.append(attributes) | |
} | |
} | |
return layoutAttributes | |
} | |
} | |
class BlogsListViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { | |
var layout: ListOffsetLayout! | |
var collectionView: UICollectionView! | |
var blogList = ["nevver", "socmoth"] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.layout = ListOffsetLayout() | |
self.layout.delegate = self | |
let historyFrame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height) | |
self.collectionView = UICollectionView(frame: historyFrame, collectionViewLayout: layout) | |
self.collectionView.delegate = self | |
self.collectionView.dataSource = self | |
self.view.backgroundColor = UIColor.white | |
self.collectionView.backgroundColor = UIColor.clear | |
self.collectionView.register(BlogViewCell.self, forCellWithReuseIdentifier: "blogViewCell") | |
self.view.addSubview(self.collectionView) | |
} | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return 1 | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return self.blogList.count | |
} | |
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "blogViewCell", for: indexPath as IndexPath) as! BlogViewCell | |
cell.titleLabel.text = self.blogList[indexPath.row] | |
return cell | |
} | |
func heightForCell(_ indexPath: IndexPath) -> CGFloat { | |
return CGFloat(44.0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment