Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 8.10 KB | None | 0 0
  1. //
  2. //  FirstViewController.swift
  3. //  Reviews
  4. //
  5. //  Created by Admin on 17/06/15.
  6. //  Copyright (c) 2015 Admin. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11.  
  12. class FeedVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
  13.  
  14.     @IBOutlet var tableView: UITableView!
  15.    
  16.     var arrayOfPosts: [Post] = [Post]()
  17.    
  18.    
  19.    
  20.     override func viewDidLoad() {
  21.          super.viewDidLoad()
  22.          self.tableView.registerClass(topCell.self, forCellReuseIdentifier: "topCell")
  23.          self.tableView.registerClass(contentCell.self, forCellReuseIdentifier: "contentCell")
  24.          tableView.registerNib(UINib(nibName: "topCell", bundle: nil), forCellReuseIdentifier: "topCell")
  25.          tableView.registerNib(UINib(nibName: "contentCell", bundle: nil), forCellReuseIdentifier: "contentCell")
  26.          self.tableView.delegate = self
  27.          self.tableView.dataSource = self
  28.          self.tableView.rowHeight = UITableViewAutomaticDimension;
  29.          self.tableView.estimatedRowHeight = 44.0;
  30.          self.setUpPost()
  31.          self.shyNavBarManager.scrollView = self.tableView;
  32.     }
  33.    
  34.  
  35.    
  36.     // This function will be called when the Dynamic Type user setting changes (from the system Settings app)
  37.     func contentSizeCategoryChanged(notification: NSNotification)
  38.     {
  39.         tableView.reloadData()
  40.     }
  41.  
  42.  
  43.     override func didReceiveMemoryWarning() {
  44.         super.didReceiveMemoryWarning()
  45.         // Dispose of any resources that can be recreated.
  46.     }
  47.  
  48.     func setUpPost(){
  49.         var post1 = Post(userName: "Dachnik", timeSincePosted: "two hours ago", profileImage: nil, posterImage: nil)
  50.         for var i = 0; i < 2; i++ {
  51.         arrayOfPosts.append(post1)
  52.         }
  53.        
  54.     }
  55.    
  56.  
  57.    
  58.  
  59.    
  60.     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  61.         let nodeCount = arrayOfPosts.count;
  62.     //    if (indexPath.row < 0)
  63.      //   {
  64.           //  let cell:topCell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
  65.           //  cell.userName.text = "Loading..."
  66.       //  }
  67.        //  else
  68.       //   {
  69.             // Leave cells empty if there's no data yet
  70.          //   if (nodeCount > 0)
  71.           //  {
  72.                 if (indexPath.row % 2 == 0){
  73.                     // Set up the cell representing the app
  74.                     let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
  75.                     let post = arrayOfPosts[indexPath.row]
  76.                     cell.userName.text = post.userName
  77.                     cell.timeSincePosted.text = post.timeSincePosted
  78.                     // Only load cached images; defer new downloads until scrolling ends
  79.                     if (post.profileImage == nil)
  80.                     {
  81.                         if (!tableView.dragging && !tableView.decelerating)
  82.                         {
  83.                             downloadProfileImage(post, indexPath: indexPath)
  84.                             cell.profileImage.image = post.profileImage
  85.                             return cell
  86.                         }
  87.                         // if a download is deferred or in progress, return a placeholder image
  88.                         cell.profileImage.image = UIImage(named: "titanic.jpg")
  89.                                            }
  90.                     else
  91.                     {
  92.                            cell.profileImage.image = post.profileImage
  93.                     }
  94.                     return cell
  95.                 }
  96.                 // Set up the cell representing the app
  97.                 let cell = tableView.dequeueReusableCellWithIdentifier("contentCell", forIndexPath: indexPath) as! contentCell
  98.                 let post = arrayOfPosts[indexPath.row]
  99.                 // Only load cached images; defer new downloads until scrolling ends
  100.                 if (post.posterImage == nil)
  101.                 {
  102.                     if (!tableView.dragging && !tableView.decelerating)
  103.                     {
  104.                        
  105.                        
  106.                         downloadPosterImage(post, indexPath: indexPath)
  107.                         cell.posterImage.image = post.posterImage
  108.                         return cell
  109.                        /*
  110.                         let url = NSURL(string: "http://www.freemovieposters.net/posters/titanic_1997_6121_poster.jpg")
  111.                         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
  112.                         cell.posterImage.image = UIImage(data: data!)
  113.                         return cell
  114.                         */
  115.                     }
  116.                     // if a download is deferred or in progress, return a placeholder image
  117.                    
  118.                  
  119.                     cell.posterImage.image = UIImage(named: "img1.jpg")
  120.                 }
  121.                 else
  122.                 {
  123.                     cell.posterImage.image = post.posterImage
  124.             }
  125.         return cell
  126.         }
  127.        
  128.   //  }
  129.        
  130.      //   return cell
  131.   //  }
  132.    
  133.    
  134.     func loadImagesForOnscreenRows(){
  135.         if (arrayOfPosts.count > 0){
  136.     let visiblePaths:NSArray = tableView.indexPathsForVisibleRows()!
  137.         for indexPath in visiblePaths {
  138.     let post = arrayOfPosts[indexPath.row]
  139.      
  140.             if (indexPath.row % 2 == 0){
  141.              //   let cell:topCell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath as! NSIndexPath) as! topCell
  142.                 let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! topCell
  143.                 if (cell.profileImage != post.profileImage){
  144.                     downloadProfileImage(post, indexPath: indexPath as! NSIndexPath)
  145.                      cell.profileImage.image = post.profileImage
  146.                 }
  147.             }
  148.  
  149.           //  let cell:contentCell = tableView.dequeueReusableCellWithIdentifier("contentCell", forIndexPath: indexPath as! NSIndexPath) as! contentCell
  150.             let cell: contentCell = tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! contentCell
  151.             if (cell.posterImage != post.posterImage){
  152.             downloadPosterImage(post, indexPath: indexPath as! NSIndexPath)
  153.              cell.posterImage.image = post.posterImage
  154.               }
  155.             }
  156.         }
  157.     }
  158.    
  159.    
  160.     func downloadProfileImage(post: Post, indexPath: NSIndexPath){
  161.      //   println(self.tableView.indexPathsForVisibleRows())
  162.       //  println(indexPath)
  163.        
  164.       //  let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath) as! topCell
  165.        
  166.        
  167.         post.profileImage = UIImage(named: "img1.jpg")
  168.     }
  169.    
  170.     func downloadPosterImage(post: Post, indexPath: NSIndexPath){
  171.         let visiblePaths:NSArray = tableView.indexPathsForVisibleRows()!
  172.         println(visiblePaths)
  173.         println(indexPath)
  174.      //   let cell: contentCell = tableView.cellForRowAtIndexPath(indexPath) as! contentCell
  175.         let url = NSURL(string: "http://www.freemovieposters.net/posters/titanic_1997_6121_poster.jpg")
  176.         let data = NSData(contentsOfURL: NSURL(string: "http://www.impawards.com/1997/posters/titanic_ver7.jpg")!)
  177.         post.posterImage = UIImage(data: data!)
  178.     }
  179.    
  180.     func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  181.         if (!decelerate){
  182.             loadImagesForOnscreenRows()
  183.             }
  184.     }
  185.    
  186.     func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
  187.         loadImagesForOnscreenRows()
  188.     }
  189.  
  190.    
  191.     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  192.         tableView.deselectRowAtIndexPath(indexPath, animated: false)
  193.     }
  194.    
  195.     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  196.         let count = arrayOfPosts.count
  197.         if (count == 0)
  198.         {
  199.             return 7;
  200.         }
  201.         return count;
  202.  
  203.     }
  204.    
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement