gpt4 book ai didi

ios - 将 TableView 的所有数据转换为 CollectionView 快速解析

转载 作者:行者123 更新时间:2023-11-28 06:50:45 24 4
gpt4 key购买 nike

为了限制对我的数据库的查询次数以及数据和电池的消耗,我想将我的 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.
}

}`

我想我必须重新考虑我的代码/项目的结构:

  • 使用我的所有变量(shopName、shopAddress、imagePromo)创建一个 swift 文件

  • 创建一个用于查询数据的 wife 文件 (query.findObjectsInBackgroundWithBlock),其中包含要调用的函数,但它涉及更改我的列表 Controller

  • 将我的列表归档

  • 归档我的收藏
    我只是不知道如何正确调用执行查询的函数...

最佳答案

在 tableViewController 的 prepareForSegue 中,您试图仅传递一个对象(detailScene.shopzcollection = object as PFObject?)。

您应该将整个对象集合 (self.objects) 传递给您的 collectionView。

我不太精通 Swift,但是一个 obj-c 的等价物是:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
....
detailScene.shopzcollection = self.objects; // shopzcollection is an NSArray in the destination as well
}

关于ios - 将 TableView 的所有数据转换为 CollectionView 快速解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34980274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com