Skip to content

Instantly share code, notes, and snippets.

@gkye
Created April 18, 2016 02:08
Show Gist options
  • Save gkye/bed12d6732d32b8ec2e26cc15ae2ecb5 to your computer and use it in GitHub Desktop.
Save gkye/bed12d6732d32b8ec2e26cc15ae2ecb5 to your computer and use it in GitHub Desktop.
//
// ProfileViewController.swift
// Drizzle
//
// Created by George on 2016-04-17.
// Copyright © 2016 George. All rights reserved.
//
import UIKit
import IBAnimatable
class TableViewCell: UITableViewCell {
@IBOutlet private weak var collectionView: UICollectionView!
}
extension TableViewCell {
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = row
collectionView.setContentOffset(collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
collectionView.reloadData()
}
var collectionViewOffset: CGFloat {
set {
collectionView.contentOffset.x = newValue
}
get {
return collectionView.contentOffset.x
}
}
}
class ProfileViewController1: UIViewController {
@IBOutlet var HeaderView: AnimatableView!
@IBOutlet var profileImg: AnimatableImageView!
@IBOutlet var followingCount: UILabel!
@IBOutlet var followersCount: UILabel!
@IBOutlet var likesCount: UILabel!
@IBOutlet var bucketsCount: UILabel!
@IBOutlet var username: UILabel!
@IBOutlet var location: UILabel!
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.section == 0){
let cell = tableView.dequeueReusableCellWithIdentifier("bioCell", forIndexPath: indexPath)
cell.textLabel?.text = "Table view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data sourceTable view data source"
cell.textLabel?.font = UIFont.systemFontOfSize(14, weight: UIFontWeightLight)
return cell
}else{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 200
return cell
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath.section == 0){
return UITableViewAutomaticDimension
}else{
tableView.estimatedRowHeight = 200
return UITableViewAutomaticDimension
}
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.section == 1){
guard let tableViewCell = cell as? TableViewCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}
}
}
extension ProfileViewController1: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.randomColor()
cell.contentView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width / 2 - 2, height: 200)
}
}
extension UIColor {
class func randomColor() -> UIColor {
let hue = CGFloat(arc4random() % 100) / 100
let saturation = CGFloat(arc4random() % 100) / 100
let brightness = CGFloat(arc4random() % 100) / 100
return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0)
}
}
@Harshad290194
Copy link

But, how can i manage multiple UICollectionView in tableviewCell of UIViewController?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment