CollectionView不显示图像

时间:2016-04-02 12:01:30

标签: ios uicollectionview uicollectionviewcell

Helllo那里!

我正在开展一个项目,你可以在地图上放置一个图钉,点击图钉并查看与该位置相关联的Flickr图像的集合视图。

一切正常,而不是实际显示图像。我有一个print - 声明,如果你点击它就会显示一个图像,但它只显示黑色背景,我无法弄清楚错误。

我将dataSource和委托连接到VC,但它仍然无法正常工作,甚至不会显示我复制到Xcode中的图像。

这是GalleryViewController

的代码
import UIKit
import Foundation
import MapKit
import CoreData

class VTGalleryVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, NSFetchedResultsControllerDelegate, Delegate, FinishedDownloadDelegate {

@IBOutlet weak var newImageSetButton: UIBarButtonItem!
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var collectionView: UICollectionView!

var pin: Pin!

var selectedImages = [NSIndexPath]()
var insertImages: [NSIndexPath]!
var deleteImages: [NSIndexPath]!
var updateImages: [NSIndexPath]!


override func viewDidLoad() {
    super.viewDidLoad()

    if FlickrImageDelegate.sharedInstance().loading(pin.location!) {
        FlickrImageDelegate.sharedInstance().addDelegate(pin.location!, delegate: self)
    } else {
        self.fetch()
        if self.pin.location!.isLoading() {
            for next in pin.location!.myImages {
                if let downloader = ImageDownload.sharedInstance().downloading[next.description.hashValue] as? ImageLoader {
                    downloader.finishedDownloadDelegate.append(self)
                }
            }
        }
    }

    if let details = self.pin.location!.details {
        self.navigationItem.title = details.locationName
    }
}

override func viewWillAppear(animated: Bool) {
    self.mapView.removeAnnotations(self.mapView.annotations)
    self.mapView.addAnnotation(self.pin)
    self.navigationItem.backBarButtonItem?.title = "Back"
    let coordinateRegion: MKCoordinateRegion = MKCoordinateRegion(center: self.pin.coordinate, span: MKCoordinateSpan(latitudeDelta: 1.0, longitudeDelta: 1.0))
    self.mapView.setRegion(coordinateRegion, animated: true)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let collectionLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    collectionLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    collectionLayout.minimumLineSpacing = 0
    collectionLayout.minimumInteritemSpacing = 0
    let length = floor(self.collectionView.frame.size.width/3)
    collectionLayout.itemSize = CGSize(width: length, height: length)

    collectionView.collectionViewLayout = collectionLayout
}

@IBAction func newImageSetButonPressed(sender: UIBarButtonItem) {
    for myImage in self.pin.location!.myImages {
        myImage.location = nil
        myImage.ownImage = nil
        self.sharedContext.deleteObject(myImage)
    }
    self.imagesForLocation()
    CDManager.sharedInstance().save()
}

func imagesForLocation() {
    FlickrImageDelegate.sharedInstance().searchImages(self.pin.location!)
    self.view.layoutIfNeeded()
    FlickrImageDelegate.sharedInstance().addDelegate(pin.location!, delegate: self)
}

func setupCell(cell: PhotoCVCell, indexPath: NSIndexPath) {
    //cell.downloadedPhoto = self.fetchResultsC.objectAtIndexPath(indexPath) as! Image
    print(cell.downloadedPhoto)
}

lazy var fetchResultsC: NSFetchedResultsController = {
    let request = NSFetchRequest(entityName: "Image")
    request.sortDescriptors = [NSSortDescriptor(key: "imagePath", ascending: true)]
    request.predicate = NSPredicate(format: "location == %@", self.pin.location!)

    let fetchController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: self.sharedContext, sectionNameKeyPath: nil, cacheName: "images")
    fetchController.delegate = self
    return fetchController
}()

lazy var sharedContext: NSManagedObjectContext = {
    return CDManager.sharedInstance().stack.managedObjectContext
}()

func fetch() {
    var error: NSError?
    NSFetchedResultsController.deleteCacheWithName("images")
    do {
        try self.fetchResultsC.performFetch()
    } catch let error1 as NSError {
        error = error1
    }
    if let _ = error {
        print(error)
    }
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return self.fetchResultsC.sections?.count ?? 0
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let info = self.fetchResultsC.sections![section]
    if let photos = self.pin!.location?.myImages where photos.count == 0 && self.isViewLoaded() && self.view.window != nil && self.newImageSetButton.enabled && !FlickrImageDelegate.sharedInstance().loading(pin.location!) {
        dispatch_async(dispatch_get_main_queue()) {
            self.imagesForLocation()
        }
    }
    return info.numberOfObjects
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let imageCell = collectionView.dequeueReusableCellWithReuseIdentifier("ImageCell", forIndexPath: indexPath) as! PhotoCVCell
    //imageCell.imageCell.image = UIImage(named: "Cabin.png")
    self.setupCell(imageCell, indexPath: indexPath)
    return imageCell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let imageCell = collectionView.cellForItemAtIndexPath(indexPath) as! PhotoCVCell
    if let index = selectedImages.indexOf(indexPath) {
        selectedImages.removeAtIndex(index)
    } else {
        selectedImages.append(indexPath)
    }
    self.setupCell(imageCell, indexPath: indexPath)
}

func controllerWillChangeContent(controller: NSFetchedResultsController) {
    insertImages = [NSIndexPath]()
    deleteImages = [NSIndexPath]()
    updateImages = [NSIndexPath]()
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch type {
    case .Insert:
        self.insertImages.append(newIndexPath!)
        break
    case .Delete:
        self.deleteImages.append(indexPath!)
        break
    case .Update:
        self.updateImages.append(indexPath!)
    default:
        break
    }
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    if self.insertImages.count > 0 {
        self.collectionView.insertItemsAtIndexPaths(self.insertImages)
    }
    if self.deleteImages.count > 0 {
        self.collectionView.deleteItemsAtIndexPaths(self.deleteImages)
    }
    if self.updateImages.count > 0 {
        self.collectionView.reloadItemsAtIndexPaths(self.updateImages)
    }
}

func searchedForLocationImages(success: Bool, location: Location, images: [Image]?, error: String?) {
    for next in pin.location!.myImages {
        if let downloader = ImageDownload.sharedInstance().downloading[next.description.hashValue] as? ImageLoader {
            downloader.finishedDownloadDelegate.append(self)
        }
    }

    dispatch_async(dispatch_get_main_queue()) {
        self.fetch()
        self.collectionView.reloadData()
        self.collectionView.layoutIfNeeded()
        self.view.layoutIfNeeded()
    }
}

func finishedLoad() {}
}

提前谢谢!

0 个答案:

没有答案