Created
August 17, 2016 18:06
-
-
Save ermst4r/51410cfb427809b18ae34b3d646d977d to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// FirstViewController.swift | |
// cheapveilingen | |
// | |
// Created by Erwin Nandpersad on 17-07-16. | |
// Copyright © 2016 Cheapveilingen.nl. All rights reserved. | |
// http://stackoverflow.com/questions/23193087/using-timer-in-a-tableview-re-creates-the-timer-after-any-scroll-or-table-reload | |
import UIKit | |
import SwiftyJSON | |
import Alamofire | |
import Kingfisher | |
import SocketIOClientSwift | |
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
@IBOutlet var tv: UITableView! | |
var productNameArray = [String]() | |
var imageUrlArray = [String]() | |
var veilingId = [Int]() | |
var loadedPages = [Int]() | |
var loadedCells = [Int]() | |
var offset = 0 | |
var limit = 10 | |
var count = 10 | |
let socket = SocketIOClient(socketURL: NSURL(string: "https://www.cheapveilingen.nl:3000")!, options: [.Log(false), .ForcePolling(true)]) | |
var timercounter = [Int]() | |
var saveSeconds = [Int]() | |
var productCounter = 0 | |
var timer: NSTimer! | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = UIColor(hexString: "#bde8fb") | |
parseXhr(offset,limit:limit) | |
self.timer = NSTimer(timeInterval: 1.0, target: self, selector: #selector(FirstViewController.fireCellsUpdate), userInfo: nil, repeats: true) | |
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes) | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
func fireCellsUpdate() { | |
let notification = NSNotification(name: "CustomCellUpdate", object: nil) | |
NSNotificationCenter.defaultCenter().postNotification(notification) | |
} | |
func parseXhr(offset: Int, limit: Int) ->Void | |
{ | |
// 1. maak hier een xhr request | |
Alamofire.request(.GET, Config.apiVeilingenUrl, parameters: ["offset": offset,"limit":self.limit]) | |
.responseJSON { response in | |
// sla hier de waarde's van de xhr request results op | |
let swiftyJson = JSON(response.result.value!) | |
let numberOfRows = swiftyJson[0].count - 1 | |
self.tv.beginUpdates() | |
// vul hier de waardes van de xhr request in een array en append deze voor globale gebruik | |
for i in 0...numberOfRows { | |
let rand = Int(arc4random_uniform(100)) | |
let productName = swiftyJson[0][i]["product_name"].string as String! | |
let imageName = swiftyJson[0][i]["product_image"].string as String! | |
//let veilingId = swiftyJson[0][i]["veiling_id"].int as Int! | |
self.productNameArray.append(productName) | |
self.imageUrlArray.append(imageName) | |
self.timercounter.append(rand) | |
// self.veilingId.append(veilingId) | |
} | |
// we maken gebruik van een offset en limit | |
// iedere keer wanneer deze methode wordt aangeroepen, moet er een nieuwe row in de tableview aangemaakt worden | |
let startOffset = (offset - self.limit < 0 ? 0 : offset - self.limit ) | |
let endOffset = (offset == 0 ? self.limit - 1 : offset - 1) // -1 hier omdat we bij de tv beginnen met 0 | |
for index in startOffset...endOffset { | |
self.tv.insertRowsAtIndexPaths([ | |
NSIndexPath(forRow: index , inSection: 0) | |
], withRowAnimation: UITableViewRowAnimation.Automatic) | |
} | |
self.tv.endUpdates() | |
} | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.productNameArray.count | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
// algemene cell gegevens | |
let cell = self.tv.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell | |
self.socket.connect() | |
let separator = UIView(frame: CGRectMake(0, 0, cell.bounds.size.width, 20)) | |
separator.backgroundColor = UIColor(hexString: "#bde8fb") | |
cell.contentView.addSubview(separator) | |
cell.photo.setBottomBorder("#bde8fb") | |
// load the image | |
cell.photo.contentMode = UIViewContentMode.ScaleAspectFit | |
cell.indexPath = indexPath.row | |
if !self.loadedCells.contains(indexPath.row) { | |
cell.timeInterval = self.timercounter[indexPath.row] | |
} | |
// | |
// self.socket.on("connect") {data, ack in | |
// self.socket.emit("veiling_id", 76) | |
// self.socket.on("start_veiling_seconds_76") { data2,ack2 in | |
// | |
// print(data2[0]) | |
// | |
// } | |
// | |
// } | |
// | |
cell.photo.kf_setImageWithURL(NSURL(string:"\(Config.image_dir)\(imageUrlArray[indexPath.row])"),placeholderImage: UIImage(named: Config.lazyLoadImage)) | |
// set the product name | |
cell.veiling_title.text = productNameArray[indexPath.row] | |
cell.veiling_title.textAlignment = .Center | |
cell.callback = { (interval) in | |
self.timercounter[indexPath.row] = interval | |
} | |
return cell | |
} | |
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { | |
// hier controleren we op welke pagina we zitten | |
// en roepen we de xhr methode aan | |
// om de cell opnieuw te laden | |
let currentLimit = self.productNameArray.count - 1 | |
let currentPage = indexPath.row / self.limit | |
if indexPath.row == currentLimit { | |
// check if the currentPage is in the array.. | |
if !self.loadedPages.contains(currentPage) { | |
self.loadedPages.append(currentPage) | |
self.offset += 1 | |
let newLimit = (self.offset * self.limit) + self.limit | |
let newOffset = (newLimit - self.limit) + self.limit | |
parseXhr(newOffset,limit: newLimit) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment