Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FirstViewController.swift
- // Reviews
- //
- // Created by Admin on 17/06/15.
- // Copyright (c) 2015 Admin. All rights reserved.
- //
- import UIKit
- class FeedVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
- @IBOutlet var tableView: UITableView!
- var arrayOfPosts: [Post] = [Post]()
- override func viewDidLoad() {
- super.viewDidLoad()
- self.tableView.registerClass(topCell.self, forCellReuseIdentifier: "topCell")
- self.tableView.registerClass(contentCell.self, forCellReuseIdentifier: "contentCell")
- tableView.registerNib(UINib(nibName: "topCell", bundle: nil), forCellReuseIdentifier: "topCell")
- tableView.registerNib(UINib(nibName: "contentCell", bundle: nil), forCellReuseIdentifier: "contentCell")
- self.tableView.delegate = self
- self.tableView.dataSource = self
- self.tableView.rowHeight = UITableViewAutomaticDimension;
- self.tableView.estimatedRowHeight = 44.0;
- self.setUpPost()
- self.shyNavBarManager.scrollView = self.tableView;
- }
- // This function will be called when the Dynamic Type user setting changes (from the system Settings app)
- func contentSizeCategoryChanged(notification: NSNotification)
- {
- tableView.reloadData()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func setUpPost(){
- var post1 = Post(userName: "Dachnik", timeSincePosted: "two hours ago", profileImage: nil, posterImage: nil)
- for var i = 0; i < 2; i++ {
- arrayOfPosts.append(post1)
- }
- }
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- let nodeCount = arrayOfPosts.count;
- // if (indexPath.row < 0)
- // {
- // let cell:topCell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
- // cell.userName.text = "Loading..."
- // }
- // else
- // {
- // Leave cells empty if there's no data yet
- // if (nodeCount > 0)
- // {
- if (indexPath.row % 2 == 0){
- // Set up the cell representing the app
- let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
- let post = arrayOfPosts[indexPath.row]
- cell.userName.text = post.userName
- cell.timeSincePosted.text = post.timeSincePosted
- // Only load cached images; defer new downloads until scrolling ends
- if (post.profileImage == nil)
- {
- if (!tableView.dragging && !tableView.decelerating)
- {
- downloadProfileImage(post, indexPath: indexPath)
- cell.profileImage.image = post.profileImage
- return cell
- }
- // if a download is deferred or in progress, return a placeholder image
- cell.profileImage.image = UIImage(named: "titanic.jpg")
- }
- else
- {
- cell.profileImage.image = post.profileImage
- }
- return cell
- }
- // Set up the cell representing the app
- let cell = tableView.dequeueReusableCellWithIdentifier("contentCell", forIndexPath: indexPath) as! contentCell
- let post = arrayOfPosts[indexPath.row]
- // Only load cached images; defer new downloads until scrolling ends
- if (post.posterImage == nil)
- {
- if (!tableView.dragging && !tableView.decelerating)
- {
- downloadPosterImage(post, indexPath: indexPath)
- cell.posterImage.image = post.posterImage
- return cell
- /*
- let url = NSURL(string: "http://www.freemovieposters.net/posters/titanic_1997_6121_poster.jpg")
- let data = NSData(contentsOfURL: NSURL(string: "http://www.impawards.com/1997/posters/titanic_ver7.jpg")!) //make sure your image in this url does exist, otherwise unwrap in a if let check
- cell.posterImage.image = UIImage(data: data!)
- return cell
- */
- }
- // if a download is deferred or in progress, return a placeholder image
- cell.posterImage.image = UIImage(named: "img1.jpg")
- }
- else
- {
- cell.posterImage.image = post.posterImage
- }
- return cell
- }
- // }
- // return cell
- // }
- func loadImagesForOnscreenRows(){
- if (arrayOfPosts.count > 0){
- let visiblePaths:NSArray = tableView.indexPathsForVisibleRows()!
- for indexPath in visiblePaths {
- let post = arrayOfPosts[indexPath.row]
- if (indexPath.row % 2 == 0){
- // let cell:topCell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath as! NSIndexPath) as! topCell
- let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! topCell
- if (cell.profileImage != post.profileImage){
- downloadProfileImage(post, indexPath: indexPath as! NSIndexPath)
- cell.profileImage.image = post.profileImage
- }
- }
- // let cell:contentCell = tableView.dequeueReusableCellWithIdentifier("contentCell", forIndexPath: indexPath as! NSIndexPath) as! contentCell
- let cell: contentCell = tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! contentCell
- if (cell.posterImage != post.posterImage){
- downloadPosterImage(post, indexPath: indexPath as! NSIndexPath)
- cell.posterImage.image = post.posterImage
- }
- }
- }
- }
- func downloadProfileImage(post: Post, indexPath: NSIndexPath){
- // println(self.tableView.indexPathsForVisibleRows())
- // println(indexPath)
- // let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath) as! topCell
- post.profileImage = UIImage(named: "img1.jpg")
- }
- func downloadPosterImage(post: Post, indexPath: NSIndexPath){
- let visiblePaths:NSArray = tableView.indexPathsForVisibleRows()!
- println(visiblePaths)
- println(indexPath)
- // let cell: contentCell = tableView.cellForRowAtIndexPath(indexPath) as! contentCell
- let url = NSURL(string: "http://www.freemovieposters.net/posters/titanic_1997_6121_poster.jpg")
- let data = NSData(contentsOfURL: NSURL(string: "http://www.impawards.com/1997/posters/titanic_ver7.jpg")!)
- post.posterImage = UIImage(data: data!)
- }
- func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
- if (!decelerate){
- loadImagesForOnscreenRows()
- }
- }
- func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
- loadImagesForOnscreenRows()
- }
- func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
- tableView.deselectRowAtIndexPath(indexPath, animated: false)
- }
- func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- let count = arrayOfPosts.count
- if (count == 0)
- {
- return 7;
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement