In order to limit the number of query to my database and consumption of data and battery, I would like to segue all the data of my TableView to my CollectionView.
import UIKit
import ParseUI
import Parse
class ListControllerView: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!)
{
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)!
// Configure the PFQueryTableView
self.parseClassName = "Shopz"
self.textKey = "shopName"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "Shopz")
// query.orderByAscending("updatedAt")
return query
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ListControllerViewCell!
if cell == nil {
cell = ListControllerViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
// Display flag image
if let finalImage = object?["imagePromo"] as? PFFile
{
finalImage.getDataInBackgroundWithBlock
{(imageData: NSData?, error: NSError?) -> Void in
if error == nil
{
if let imageData = imageData
{
cell.ImagePromo!.image = UIImage(data:imageData)
}
}
}
}
// Extract values from the PFObject to display in the table cell
if let shopnamelabel = object?["shopName"] as? String
{
cell.ShopName!.text = shopnamelabel
print(shopnamelabel)
}
if let shopaddresslabel = object?["shopAddress"] as? String
{
cell.ShopAddress!.text = shopaddresslabel
print(shopaddresslabel)
}
return cell;
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
var detailScene = segue.destinationViewController as! CollectionView
detailScene.shopzcollection = object as PFObject?
}
override func viewDidAppear(animated: Bool) {
// Refresh the table to ensure any data changes are displayed
tableView.reloadData();
}
}`
collection view controller:
import UIKit
import Parse
import ParseUI
var shopzcollection = [PFObject]()`
class CollectionView: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
// Connection to the collection view
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
/*
==========================================================================================
Ensure data within the collection view is updated when ever it is displayed
==========================================================================================
*/
// Load data into the collectionView when the view appears
override func viewDidAppear(animated: Bool) {
loadCollectionViewData()
}
/*
==========================================================================================
Fetch data from the Parse platform
==========================================================================================
*/
func loadCollectionViewData() {
// Build a parse query object
var query = PFQuery(className:"Shopz")
// Fetch data from the parse platform
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
// The find succeeded now rocess the found objects into the countries array
if error == nil {
// Clear existing data
shopzcollection.removeAll(keepCapacity: true)
// Add shop objects to our array
if let objects = objects as [PFObject]? {
shopzcollection = Array(objects.generate())
}
// reload our data into the collection view
self.collectionView.reloadData()
} else {
// Log details of the failure
print("Error: \(error!) ")
}
}
}
/*
==========================================================================================
UICollectionView protocol required methods
==========================================================================================
*/
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return shopzcollection.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! CollectionViewCell
// Display the country name
if let value = shopzcollection[indexPath.row]["shopName"] as? String {
cell.cellTitle.text = value
}
if let value = shopzcollection[indexPath.row]["shopAddress"] as? String {
cell.cellDetail.text = value
}
// Fetch final flag image - if it exists
if let finalImage = shopzcollection[indexPath.row]["imagePromo"] as? PFFile {
finalImage.getDataInBackgroundWithBlock
{(imageData: NSData?, error: NSError?) -> Void in
if error == nil
{
if let imageData = imageData
{
cell.cellImage!.image = UIImage(data:imageData)
}
}
}
}
return cell
}
/*
==========================================================================================
Process memory issues
To be completed
==========================================================================================
*/
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}`
I guess I have to think again about the structure of my code/project:
create a swift file with all my var ( shopName, shopAddress, imagePromo)
create a wife file for the query to the data ( query.findObjectsInBackgroundWithBlock) with a function to call but it involves to change my list controller
file with my list
file with my collection
I just don't know how to call properly the function which executes the query...
In the prepareForSegue
of your tableViewController, you are trying to pass only one object ( detailScene.shopzcollection = object as PFObject?
).
You should pass the entire collection of objects ( self.objects
) to your collectionView.
I'm not well versed with Swift, but an obj-c equivalent would be:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
....
detailScene.shopzcollection = self.objects; // shopzcollection is an NSArray in the destination as well
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.