lekanadeyeri

Untitled

Mar 23rd, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.03 KB | None | 0 0
  1. // Houses data model
  2. import Foundation
  3.  
  4. class Houses : NSObject{
  5.  
  6.     var houseName: String?
  7.     var UsersInHouse = [BackendlessUser]()
  8.     var ownerId: String?
  9.     var objectId : String?
  10. }
  11. ________________________________________________________________________________________
  12. //  Post data model
  13. import Foundation
  14.  
  15. class Posts : NSObject {
  16.    
  17.     var Author: BackendlessUser?
  18.     var HousePostBelongsTo: Houses?
  19.     var PostText: String?
  20.     var PostVoteCount: Int = 0
  21.     var ownerId: String?
  22.     var objectId: String?
  23.     var imageURL: String?
  24. }
  25. ________________________________________________________________________________________
  26.  
  27. //
  28. //  HousesTableViewController.swift
  29. //  HousesApp
  30. //
  31. //  Created by Lekan Adeyeri on 3/9/16.
  32. //  Copyright © 2016 Lekan Adeyeri. All rights reserved.
  33. //
  34.  
  35. import UIKit
  36.  
  37. class HousesTableViewController: UITableViewController {
  38.    
  39.    
  40.     let APP_ID = "IGNORE"
  41.     let SECRET_KEY = "IGNORE"
  42.     let VERSION_NUM = "v1"
  43.    
  44.    
  45.    
  46.     var backendless = Backendless.sharedInstance()
  47.     var houses: [Houses]! = []
  48.     var error: Fault?
  49.  
  50.  
  51.    
  52.     @IBOutlet weak var table: UITableView!
  53.  
  54.    
  55.     override func viewDidLoad() {
  56.         super.viewDidLoad()
  57.        
  58.        backendless.initApp(APP_ID, secret:SECRET_KEY, version:VERSION_NUM)
  59.         self.backendless.userService.getPersistentUser()
  60.         let user = self.backendless.userService.currentUser
  61.         if (user == nil) {
  62.             Utilities.loginUser(self)
  63.         } else {
  64.             self.loadData()
  65.         }
  66.        
  67.         findHousesUserIsIn()
  68.        
  69.         self.table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
  70.         self.table.dataSource = self
  71.         self.table.delegate = self
  72.  
  73.         // Uncomment the following line to preserve selection between presentations
  74.         // self.clearsSelectionOnViewWillAppear = false
  75.  
  76.         // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  77.         // self.navigationItem.rightBarButtonItem = self.editButtonItem()
  78.     }
  79.    
  80.      func findHousesUserIsIn() {
  81.        
  82.         self.backendless.userService.getPersistentUser()
  83.         let user = self.backendless.userService.currentUser
  84.  
  85.        
  86.         let whereClause = "UsersInHouse.objectId = '\(user.objectId)'"
  87.         let dataQuery = BackendlessDataQuery()
  88.         dataQuery.whereClause = whereClause
  89.        
  90.  
  91.         let houseObjects = self.backendless.persistenceService.find(Houses.ofClass(),
  92.             dataQuery:dataQuery) as BackendlessCollection
  93.        
  94.        
  95.         if error == nil {
  96.             self.houses.appendContentsOf(houseObjects.data as! [Houses]!)
  97.             for house in houseObjects.data as! [Houses] {
  98.                 print(house.houseName)
  99.  
  100.             }
  101.            
  102.         }
  103.         else {
  104.             print("Server reported an error: \(error)")
  105.         }
  106.     }
  107.  
  108.     func loadData() {
  109.         var houses: [Houses]! = []
  110.        
  111.     }
  112.     override func didReceiveMemoryWarning() {
  113.         super.didReceiveMemoryWarning()
  114.         // Dispose of any resources that can be recreated.
  115.     }
  116.  
  117.     // MARK: - Table view data source
  118.    
  119.  
  120.     override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  121.         // #warning Incomplete implementation, return the number of sections
  122.         return 1
  123.     }
  124.  
  125.     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  126.         // #warning Incomplete implementation, return the number of rows
  127.         return houses.count
  128.     }
  129.    
  130.  
  131.     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  132.        
  133.        
  134.         let cell = tableView.dequeueReusableCellWithIdentifier("houseCell") as! HousesCell
  135.         cell.bindData(self.houses[indexPath.row])
  136.    
  137.         // Configure the cell...
  138.  
  139.         return cell
  140.     }
  141.    
  142.     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  143.         tableView.deselectRowAtIndexPath(indexPath, animated: true)
  144.        
  145.         let house = self.houses[indexPath.row]
  146.         let houseId = house.objectId! as String
  147.         print(houseId)
  148.    
  149.     }
  150.  
  151. }
  152. ________________________________________________________________________________________
  153. //
  154. //  HousesCell.swift
  155. //  HouseApp2
  156. //
  157. //  Created by Lekan Adeyeri on 3/21/16.
  158. //  Copyright © 2016 Lekan Adeyeri. All rights reserved.
  159. //
  160.  
  161. import UIKit
  162.  
  163. class HousesCell: UITableViewCell, UIScrollViewDelegate {
  164.    
  165.     @IBOutlet var houseNameText: UILabel!
  166.  
  167.     override func awakeFromNib() {
  168.         super.awakeFromNib()
  169.         // Initialization code
  170.     }
  171.  
  172.     override func setSelected(selected: Bool, animated: Bool) {
  173.         super.setSelected(selected, animated: animated)
  174.  
  175.         // Configure the view for the selected state
  176.     }
  177.  
  178.     func bindData(house: Houses) {
  179.  
  180.         self.houseNameText.text = house.houseName! as String
  181.        
  182.     }
  183. }
Add Comment
Please, Sign In to add comment