[英]Segue all the data of a TableView to a CollectionView swift parse
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. 为了限制对数据库的查询数量以及数据和电池的消耗,我想将TableView的所有数据都隔离到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) 使用我所有的变量(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 使用要调用的函数为数据查询创建一个妻子文件(query.findObjectsInBackgroundWithBlock),但这涉及更改我的列表控制器
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?
). 在tableViewController的prepareForSegue
中,您尝试仅传递一个对象( detailScene.shopzcollection = object as PFObject?
)。
You should pass the entire collection of objects ( self.objects
) to your collectionView. 您应该将整个对象集合( self.objects
)传递给collectionView。
I'm not well versed with Swift, but an obj-c equivalent would be: 我不太熟悉Swift,但是obj-c等效项是:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
....
detailScene.shopzcollection = self.objects; // shopzcollection is an NSArray in the destination as well
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.