【发布时间】:2015-05-25 11:57:04
【问题描述】:
我有大约 150 张图像的 Parse.com 查询,填充了 UICollectionView。图像根据用户点击以进入 UICollectionView 的按钮进行更改。点击后,集合开始下载,您很快就可以看到前三四张图片。
但是,当您滚动时,您会看到已下载的相同图像被复制并四处移动,并用尚未下载的图像切换位置。最终他们都下载了,但需要很长时间。 (比如 90 秒。)除此之外,图像不会开始加载,直到用户滚动到图像的位置并等待它。
我是一名初学者开发人员,所以我不知道这是一个明显的修复还是需要一些认真的工作。我的代码在这里:
UICollectionView 代码:
var exp = ""
class CollectionCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
images = []
parseObjects = []
imageNames = []
imageExpansions = []
var downloadCards = PFQuery(className: "Cards")
downloadCards.whereKey("ExpansionNumber", equalTo:"\(exp)")
downloadCards.limit = 200
downloadCards.orderByAscending("Number")
downloadCards.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
println("Successfully retrieved \(objects!.count) cards.")
if let objects = objects as? [PFObject] {
for object in objects {
parseObjects.append(object["Image"] as! PFFile)
imageNames.append(object["Number"] as! String)
imageExpansions.append(object["ExpansionNumber"] as! String)
self.collectionView.reloadData()
}
}
} else {
println("Error: \(error!) \(error!.userInfo!)")
}
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return parseObjects.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: CardsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CardsCollectionViewCell
parseObjects[indexPath.row].getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.cardsImg.image = image
}
}
//cell.cardLabel.text = imageNames[indexPath.row]
return cell
}
}
此处的按钮搜索/查询标识符代码:
var images = [UIImage]()
var parseObjects = [PFFile]()
var imageNames = [String]()
var imageExpansions = [String]()
class selectExpansionViewController: UIViewController {
@IBAction func xy1Button(sender: AnyObject) {
exp = "59"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func xy2Button(sender: AnyObject) {
exp = "60"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func xy3Button(sender: AnyObject) {
exp = "61"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func xy4Button(sender: AnyObject) {
exp = "62"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func xy5Button(sender: AnyObject) {
exp = "63"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func xydcButton(sender: AnyObject) {
exp = "63-2"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func xy6Button(sender: AnyObject) {
exp = "64"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw1Button(sender: AnyObject) {
exp = "48"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw2Button(sender: AnyObject) {
exp = "49"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw3Button(sender: AnyObject) {
exp = "50"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw4Button(sender: AnyObject) {
exp = "51"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw5Button(sender: AnyObject) {
exp = "52"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw6Button(sender: AnyObject) {
exp = "53"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw7Button(sender: AnyObject) {
exp = "54"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw8Button(sender: AnyObject) {
exp = "55"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw9Button(sender: AnyObject) {
exp = "56"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw10Button(sender: AnyObject) {
exp = "57"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func bw11Button(sender: AnyObject) {
exp = "58"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func hgss1Button(sender: AnyObject) {
exp = "43"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func hgss2Button(sender: AnyObject) {
exp = "44"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
@IBAction func hgss3Button(sender: AnyObject) {
exp = "45"
self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}
【问题讨论】:
标签: ios swift parse-platform uicollectionview