Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//
// SampleView.swift
// ss
//
// Created by Noitidart Saab on 6/18/17.
// Copyright © 2017 Facebook. All rights reserved.
//
import Foundation
import UIKit
import SpreadsheetView
class ViewController: UIViewController, SpreadsheetViewDataSource, SpreadsheetViewDelegate {
let spreadsheetView = SpreadsheetView()
let dates = ["7/10/2017", "7/11/2017", "7/12/2017", "7/13/2017", "7/14/2017", "7/15/2017", "7/16/2017"]
let days = ["MONDAY", "TUESDAY", "WEDNSDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]
let dayColors = [UIColor(red: 0.918, green: 0.224, blue: 0.153, alpha: 1),
UIColor(red: 0.106, green: 0.541, blue: 0.827, alpha: 1),
UIColor(red: 0.200, green: 0.620, blue: 0.565, alpha: 1),
UIColor(red: 0.953, green: 0.498, blue: 0.098, alpha: 1),
UIColor(red: 0.400, green: 0.584, blue: 0.141, alpha: 1),
UIColor(red: 0.835, green: 0.655, blue: 0.051, alpha: 1),
UIColor(red: 0.153, green: 0.569, blue: 0.835, alpha: 1)]
let hours = ["6:00 AM", "7:00 AM", "8:00 AM", "9:00 AM", "10:00 AM", "11:00 AM", "12:00 AM", "1:00 PM", "2:00 PM",
"3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM", "9:00 PM", "10:00 PM", "11:00 PM"]
let evenRowColor = UIColor(red: 0.914, green: 0.914, blue: 0.906, alpha: 1)
let oddRowColor: UIColor = .white
let data = [
["", "", "Take medicine", "", "", "", "", "", "", "", "", "", "", "Movie with family", "", "", "", "", "", ""],
["Leave for cabin", "", "", "", "", "Lunch with Tim", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
["", "", "", "", "Downtown parade", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Fireworks show", "", "", ""],
["", "", "", "", "", "Family BBQ", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", "", "", "", "", "", "", "Return home", "", "", "", "", "", ""]
]
override func viewDidLoad() {
super.viewDidLoad()
spreadsheetView.dataSource = self
spreadsheetView.delegate = self
spreadsheetView.contentInset = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)
spreadsheetView.intercellSpacing = CGSize(width: 4, height: 1)
spreadsheetView.gridStyle = .none
spreadsheetView.register(DateCell.self, forCellWithReuseIdentifier: String(describing: DateCell.self))
spreadsheetView.register(TimeTitleCell.self, forCellWithReuseIdentifier: String(describing: TimeTitleCell.self))
spreadsheetView.register(TimeCell.self, forCellWithReuseIdentifier: String(describing: TimeCell.self))
spreadsheetView.register(DayTitleCell.self, forCellWithReuseIdentifier: String(describing: DayTitleCell.self))
spreadsheetView.register(ScheduleCell.self, forCellWithReuseIdentifier: String(describing: ScheduleCell.self))
self.view = spreadsheetView
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
spreadsheetView.flashScrollIndicators()
}
// MARK: DataSource
func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int {
return 1 + days.count
}
func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int {
return 1 + 1 + hours.count
}
func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat {
if case 0 = column {
return 70
} else {
return 120
}
}
func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat {
if case 0 = row {
return 24
} else if case 1 = row {
return 32
} else {
return 40
}
}
func frozenColumns(in spreadsheetView: SpreadsheetView) -> Int {
return 1
}
func frozenRows(in spreadsheetView: SpreadsheetView) -> Int {
return 2
}
func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell? {
if case (1...(dates.count + 1), 0) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: DateCell.self), for: indexPath) as! DateCell
cell.label.text = dates[indexPath.column - 1]
return cell
} else if case (1...(days.count + 1), 1) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: DayTitleCell.self), for: indexPath) as! DayTitleCell
cell.label.text = days[indexPath.column - 1]
cell.label.textColor = dayColors[indexPath.column - 1]
return cell
} else if case (0, 1) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: TimeTitleCell.self), for: indexPath) as! TimeTitleCell
cell.label.text = "TIME"
return cell
} else if case (0, 2...(hours.count + 2)) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: TimeCell.self), for: indexPath) as! TimeCell
cell.label.text = hours[indexPath.row - 2]
cell.backgroundColor = indexPath.row % 2 == 0 ? evenRowColor : oddRowColor
return cell
} else if case (1...(days.count + 1), 2...(hours.count + 2)) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: ScheduleCell.self), for: indexPath) as! ScheduleCell
let text = data[indexPath.column - 1][indexPath.row - 2]
if !text.isEmpty {
cell.label.text = text
let color = dayColors[indexPath.column - 1]
cell.label.textColor = color
cell.color = color.withAlphaComponent(0.2)
cell.borders.top = .solid(width: 2, color: color)
cell.borders.bottom = .solid(width: 2, color: color)
} else {
cell.label.text = nil
cell.color = indexPath.row % 2 == 0 ? evenRowColor : oddRowColor
cell.borders.top = .none
cell.borders.bottom = .none
}
return cell
}
return nil
}
/// Delegate
func spreadsheetView(_ spreadsheetView: SpreadsheetView, didSelectItemAt indexPath: IndexPath) {
print("Selected: (row: \(indexPath.row), column: \(indexPath.column))")
}
}
class SampleView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
label.text = "This is Swift"
self.addSubview(label)
let controller = ViewController();
self.addSubview(controller.view);
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment