在collectionviewcontroller中单击图像时,在detailviewcontroller中获取图像详细信息

时间:2015-04-21 14:25:25

标签: ios xcode swift

我的CollectionViewController包含一个图像。当我单击CollectionViewController中的图像时,它会将我带到detailviewcontroller,我想在其中编辑图像的细节。当我输入URL等详细信息,然后单击后退按钮时,它应该从URL获取图像并替换我的CollectionViewController中的图像。

我有两个问题:

  • 当我单击CollectionViewController中的图像时,它会打开detailviewcontroller,但它不会显示我单击的图像的URL。
  • 当我单击后退按钮时,它不会将我带回CollectionViewController。
import Foundation

class Photo {
  var url: String = ""
  var data: NSData? = nil
  var Title: String = ""
  var Tags: String = ""

  func loadImage(completionHandler: (data: NSData?) ->Void) {
    let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
    dispatch_async(queue) {
      let mainQueue = dispatch_get_main_queue()
      if let url = NSURL(string: self.url) {
        if let data = NSData(contentsOfURL: url) {
          sleep(3)
          dispatch_async(mainQueue) {
            self.data = data
            completionHandler(data: data)
          }
          return
        }

        dispatch_async(mainQueue) {
          completionHandler(data: nil)
        }
      }
    }
  }
}

detailviewcontroller

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

  var photo: Photo!

  @IBOutlet weak var textURL: UITextField!

  @IBOutlet weak var textTitle: UITextField!
  @IBOutlet weak var textTags: UITextField!
  @IBOutlet weak var imageView: UIImageView!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  func textFieldShouldReturn(textField: UITextField) -> Bool {
    let urlString = textField.text
    photo.url = urlString
    let DatatoImage: (NSData?) -> Void = {
      if let d = $0 {
        let image = UIImage(data: d)
        self.imageView.image = image
      } else {
        self.imageView.image = nil
      }
      //textField.resignFirstResponder()
    }
    if let d = photo.data {
      DatatoImage(d)
      textField.resignFirstResponder()
      let image = UIImage(data: d)
      self.imageView.image = image
    } else {
      photo.loadImage(DatatoImage)
    }
    return true
  }
}

我的masterViewController

import UIKit

let reuseIdentifier = "Cell"

class PhotosCollectionViewController: UICollectionViewController {

  var photos = Array<Photo>()

  override func viewDidLoad() {
    super.viewDidLoad()
    let photo = Photo()
    photo.url = "http://www.griffith.edu.au/__data/assets/image/0019/632332/gu-header-logo.png"

    photos.append(photo)

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    self.collectionView!.registerClass(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }

  override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 1
  }


  override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return photos.count
  }

  override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as PhotoCollectionViewCell

    let photo = photos[indexPath.row]
    if let d = photo.data {
      let image = UIImage(data: d)
      cell.imageView.image = image

      photo.url = "\(photo.url)"
      photo.Title = "\(photo.Title)"
    } else {
      photo.loadImage {
        if $0 != nil {
          collectionView.reloadItemsAtIndexPaths([indexPath])
        }
      }
    }
    return cell
  }

  // Uncomment this method to specify if the specified item should be highlighted during tracking
  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let indexPath = sender as? NSIndexPath {
      let photo = photos[indexPath.row]
    }
    if (segue.identifier == "showDetail") {
      if let dvc = segue.destinationViewController as? ViewController {
        let photo = Photo()
        dvc.photo = photo
      }
    }
  }

  override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    performSegueWithIdentifier("showDetail", sender: indexPath)
    return true
  }

1 个答案:

答案 0 :(得分:0)

在prepareForSegue方法中,您有这一行,

let photo = Photo()

这将创建一个新的Photo对象,它不会获得您在所选单元格中显示的对象。你在if let语句中引用了正确的引用。使用那个(let photo = photos[indexPath.row])。

对于后退按钮,如果后退按钮是您推送到新控制器时自动获得的按钮,则不必编写任何代码来使其工作。