Skip to content

Instantly share code, notes, and snippets.

@johndelong
Last active October 19, 2016 17:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save johndelong/5f079d9e40111bce1110c3e945329cb3 to your computer and use it in GitHub Desktop.
Infinite Scrolling Setup
//
// ViewController.swift
// Example
//
// Created by John DeLong on 5/11/16.
// Copyright © 2016 delong. All rights reserved.
//
import UIKit
extension Date {
func dateFromDays(_ days: Int) -> Date {
return (Calendar.current as NSCalendar).date(byAdding: .day, value: days, to: self, options: [])!
}
}
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let cellHeight: CGFloat = 44
let dateFormatter = DateFormatter()
lazy var days: [Date] = {
let beginDate = Date().dateFromDays(-29)
let endDate = Date().dateFromDays(30)
return self.generateDays(beginDate, endDate: endDate)
}()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.dateFormatter.dateFormat = "MM-dd-yyyy"
}
func generateDays(_ beginDate: Date, endDate: Date) -> [Date] {
var dates: [Date] = []
var date = beginDate
while date.compare(endDate) != .orderedDescending {
dates.append(date)
date = date.dateFromDays(1)
}
return dates
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.days.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel!.text = self.dateFormatter.string(from: self.days[(indexPath as NSIndexPath).row])
return cell
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.cellHeight;
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// TODO: Add infinite scrolling functionality
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment