所以这是我在这里发表的第一篇文章,我现在已经在swift中开发了几个月,而且无法解决这个问题。
我试过在我的图片中解释我需要帮助的内容。
如果我点击tableview第1行,我想要将30-70个图像的数组发送到集合视图,这种方法有效,除了它在所有行上显示相同的图像,tableview行2 -3 -4。 image of collectionView 请查看我的解析数据库的此链接 parse database
我非常乐意与bitbucket分享。
//Here goes the TableView
import UIKit
import Parse
class TableViewController:PFQueryTableViewController {
var selectedRow:PFUser?
var userids = [""]
var wonk = ["wonk"]
var slagtHuset = ["slagtHuset"]
var Adelgatan = ["Adelgatan"]
// 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 = "crowd"
self.textKey = "cellTitle"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
// self.objectsPerPage = 3 }
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "crowd")
query.orderByDescending("cellTitle")
// Only retrieve the last ten
query.limit = 10
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("CustomCell") as! CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
// Extract values from the PFObject to display in the table cell
if let nameEnglish = object?["cellTitle"] as? String {
cell.customNameEnglish.text = nameEnglish
}
// Display flag image
var initialThumbnail = UIImage(named: "question")
cell.customFlag.image = initialThumbnail
if let thumbnail = object?["crowdImage"] as? PFFile {
cell.customFlag.file = thumbnail
cell.customFlag.loadInBackground()
}
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! DetailViewController
// Pass the selected object to the destination view controller.
if let indexPath = self.tableView?.indexPathForSelectedRow {
let row = Int(indexPath.row)
detailScene.currentObject = objects?[row] as? PFObject
//detailScene.currentObject = [Adelgatan, slagtHuset]
// NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow]; // DOG details =(DOG )segue.destinationViewController;
// PFObject * object = [self.typeDog objectAtIndex:indexPath.row]; // details.stringdata = [object objectForKey:@“username”];
// pass whatever data you want to display in the collection view as an array here.
// details.array = ...
}
}
override func viewDidAppear(animated: Bool) {
// Refresh the table to ensure any data changes are displayed
tableView.reloadData()
}
这是detailView(collectionView)的代码
import UIKit
var countries = PFObject
class DetailViewController:UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
// Container to store the view table selected object
var currentObject : PFObject?
var wonk = ["wonk"]
var slagtHuset = ["slagtHuset"]
var Adelgatan = ["Adelgatan"]
// var clubs = [slagtHuset,Adelgatan,wonk]
// Some text fields
@IBOutlet var collectionView: UICollectionView!
@IBOutlet weak var flag: PFImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Resize size of collection view items in grid so that we achieve 3 boxes across
let cellWidth = ((UIScreen.mainScreen().bounds.width) - 3 - 3 ) / 2
let cellHeight = ((UIScreen.mainScreen().bounds.width) - 4 - 3 ) / 2
let cellLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
cellLayout.itemSize = CGSize(width: cellWidth, height: cellHeight)
}
func loadCollectionViewData() {
// Build a parse query object
var query = PFQuery(className:"crowd")
//more specs query.whereKey("currencyCode", equalTo:"EUR")
query.orderByAscending("objectId")
// Fetch data from the parse platform
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
// The find succeeded now rocess the found objects into the countries array
if error == nil {
// Clear existing country data
countries.removeAll(keepCapacity: true)
// Add country objects to our array
if let objects = objects as? [PFObject] {
countries = Array(objects.generate())
}
// reload our data into the collection view
self.collectionView.reloadData()
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(countries)
return countries.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
// Display "initial" flag image
var initialThumbnail = UIImage(named: "question")
cell.cellImage.image = initialThumbnail
// Here is my problem where i only load image from Adelgatan
// Fetch final flag image - if it exists
if let value = countries[indexPath.row]["Adelgatan"] as? PFFile {
let finalImage = countries[indexPath.row]["Adelgatan"] 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