Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.32 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. class FeedVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
  12.  
  13.     @IBOutlet var tableView: UITableView!
  14.     var arrayOfPosts: [Post] = [Post]()
  15.     var offset = CGPoint()
  16.    
  17.     override func viewDidLoad() {
  18.          super.viewDidLoad()
  19.          self.tableView.registerClass(topCell.self, forCellReuseIdentifier: "topCell")
  20.          self.tableView.registerClass(contentCell.self, forCellReuseIdentifier: "contentCell")
  21.          tableView.registerNib(UINib(nibName: "topCell", bundle: nil), forCellReuseIdentifier: "topCell")
  22.          tableView.registerNib(UINib(nibName: "contentCell", bundle: nil), forCellReuseIdentifier: "contentCell")
  23.          self.tableView.delegate = self
  24.          self.tableView.dataSource = self
  25.          self.tableView.rowHeight = UITableViewAutomaticDimension;
  26.          self.tableView.estimatedRowHeight = 44.0;
  27.          self.setUpPost()
  28.          self.shyNavBarManager.scrollView = self.tableView;
  29.     }
  30.    
  31.   //  override func viewDidAppear(animated: Bool) {
  32.   //      tableView.setContentOffset((CGPointMake(0, 23)), animated: false)
  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 < 100; 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. /*
  63.         if (indexPath.row == 0)
  64.         {
  65.             let cell:topCell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
  66.             cell.userName.text = "Loading..."
  67.         }
  68.          else
  69.          {
  70.             // Leave cells empty if there's no data yet
  71.             if (nodeCount > 0)
  72.             {
  73. */
  74.                 if (indexPath.row % 2 == 0){
  75.                     // Set up the cell representing the app
  76.                     let cell = tableView.dequeueReusableCellWithIdentifier("topCell", forIndexPath: indexPath) as! topCell
  77.                     cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0); // has to be with 0.f ?
  78.                     let post = arrayOfPosts[indexPath.row]
  79.                     cell.userName.text = post.userName
  80.                     cell.timeSincePosted.text = post.timeSincePosted
  81.                     // Only load cached images; defer new downloads until scrolling ends
  82.                    // if (post.profileImage == nil)
  83.                    // {
  84.                         if (!tableView.dragging && !tableView.decelerating)
  85.                         {
  86.                             cell.profileImage.setImageWithUrl(NSURL(string: "http://da4nikam.ru/wp-content/uploads/2010/12/e5_1_b.jpg")!, placeHolderImage: nil)
  87.                             return cell
  88.                                            }
  89.                     return cell
  90.                 }
  91.                 // Set up the cell representing the app
  92.                 let cell = tableView.dequeueReusableCellWithIdentifier("contentCell", forIndexPath: indexPath) as! contentCell
  93.                 let post = arrayOfPosts[indexPath.row]
  94.                     if (!tableView.dragging && !tableView.decelerating)
  95.                     {
  96.                         cell.posterImage.setImageWithUrl(NSURL(string: "http://www.freemovieposters.net/posters/titanic_1997_6121_poster.jpg")!, placeHolderImage: nil)
  97.                         return cell
  98.                 }
  99.         return cell
  100.         }
  101.        
  102.   //  }
  103.        
  104.      //   return cell
  105.   //  }
  106.    
  107.    
  108.     func loadImagesForOnscreenRows(){
  109.         if (arrayOfPosts.count > 0){
  110.     let visiblePaths:NSArray = tableView.indexPathsForVisibleRows()!
  111.         for indexPath in visiblePaths {
  112.     let post = arrayOfPosts[indexPath.row]
  113.             if (indexPath.row % 2 == 0){
  114.                 Async.background(){
  115.                 let cell:topCell = self.tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! topCell
  116.                 cell.profileImage.setImageWithUrl(NSURL(string: "http://da4nikam.ru/wp-content/uploads/2010/12/e5_1_b.jpg")!, placeHolderImage: nil)
  117.                 }
  118.             }
  119.            else{
  120.                    Async.background(){
  121.             let cell: contentCell = self.tableView.cellForRowAtIndexPath(indexPath as! NSIndexPath) as! contentCell
  122.                   let url = NSURL(string: "http://www.freemovieposters.net/posters/titanic_1997_6121_poster.jpg")
  123.                   cell.posterImage.setImageWithUrl(url!, placeHolderImage: nil)
  124.                     }
  125.             }
  126.             }
  127.         }
  128.     }
  129.    
  130.     func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  131.         if (!decelerate){
  132.             loadImagesForOnscreenRows()
  133.             }
  134.     }
  135.    
  136.     func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
  137.         loadImagesForOnscreenRows()
  138.     }
  139.  
  140.    
  141.     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  142.         performSegueWithIdentifier("showDetailedPost", sender: nil)
  143.         tableView.deselectRowAtIndexPath(indexPath, animated: false)
  144.     }
  145.    
  146.     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  147.         let count = arrayOfPosts.count
  148.         if (count == 0)
  149.         {
  150.             return 7;
  151.         }
  152.         return count;
  153.  
  154.     }
  155.    
  156.     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  157.         if segue.identifier == "showDetailedPost" {
  158.             if let vc = segue.destinationViewController as? DetailedPostVC {
  159.                 offset = tableView.contentOffset
  160.                     vc.num = tableView.indexPathForSelectedRow()!.row
  161.             }
  162.         }
  163.     }
  164.    
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement