Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nazmulkp/c3c866a181fa7ac6825eae5632f76c36 to your computer and use it in GitHub Desktop.
Save nazmulkp/c3c866a181fa7ac6825eae5632f76c36 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// PagginationSwift3
//
// Created by Nazmul Hasan on 8/2/17.
// Copyright © 2017 Nazmul hasan. All rights reserved.
//
import UIKit
class Student{
var name : String = ""
}
class NewsViewController: UIViewController {
let newsfeedCellId = "newsfeedCellId"
var tableViewIndex : IndexPath?
var students : [[Student]]?
var count = 0
var firstIndex = 0
var itemNumber = 0
var uploadedSection : Int?
// variable to save the last position visited, default to zero
fileprivate var lastContentOffset: CGFloat = 0
override func viewDidLoad() {
self.registerViews()
self.setupViews()
students = [[Student]]()
initUpdateUI()
}
lazy var newsFeedTableView : UITableView = {
var tableView = UITableView()
tableView.backgroundColor = UIColor.white
tableView.dataSource = self
tableView.separatorStyle = .none
tableView.delegate = self
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
}
extension NewsViewController {
func setupViews() {
self.view.addSubview(newsFeedTableView)
self.newsFeedTableView.fillSuperview()//rander the full screen
}
func registerViews() {
newsFeedTableView.register(NewsTableViewViewCell.self, forCellReuseIdentifier: newsfeedCellId)
}
}
extension NewsViewController : UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return students?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return students?[section].count ?? 0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let indexpath = tableViewIndex else { return }
if self.lastContentOffset > scrollView.contentOffset.y {
//scrolling up
//adding top of the array
//TODO FIX
} else if self.lastContentOffset < scrollView.contentOffset.y {
//scrolling down
//adding bottom of the array
if let _students = students?[indexpath.section]{
if _students.count / 2 == indexpath.row + 1 {
if uploadedSection != indexpath.section {
uploadedSection = indexpath.section
self.oldestState()
}
}
}
}
self.lastContentOffset = scrollView.contentOffset.y
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: newsfeedCellId, for: indexPath) as! NewsTableViewViewCell
tableViewIndex = indexPath
if let _states = students?[indexPath.section]{
cell.student = _states[indexPath.row]
cell.backgroundColor = UIColor.blue
}
return cell
}
}
extension NewsViewController : UITableViewDelegate {
}
//MARK:- Load Data to the UI
extension NewsViewController {
func initUpdateUI(){
self.students?.append(getdata())
self.newsFeedTableView.reloadData()
}
//scrolling up time calling this method
func newestState(){
//todo ..
}
//scrolling down time calling this method
func oldestState(){
self.newsFeedTableView.beginUpdates()
self.students?.append(getdata()) //Update state property
let indexPaths = (0..<(getdata().count)).map { IndexPath(row: $0, section: (self.students?.count)! - 1) }
print(indexPaths)
self.newsFeedTableView.insertSections([(self.students?.count)! - 1], with: UITableViewRowAnimation.automatic)
self.newsFeedTableView.insertRows(at: indexPaths, with: UITableViewRowAnimation.automatic)
self.newsFeedTableView.endUpdates()
}
func getdata() -> [Student]{
var _students = [Student]()
for i in itemNumber..<(itemNumber + 4) {
let student = Student()
student.name = "\(i)"
print("adding student roll number : \(student.name)")
_students.append(student)
}
itemNumber += 4
return _students
}
}
class NewsTableViewViewCell : BaseTableViewCell{
var student : Student?{
didSet{
statesTextView.text = student?.name
}
}
lazy var statesTextView: UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
override func setupViews() {
super.setupViews()
addSubview(statesTextView)
statesTextView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 5, leftConstant: 10, bottomConstant: 2, rightConstant: 5, widthConstant: 0, heightConstant: 0)
}
}
class BaseTableViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
print("calling cell baseTableCell")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment