如何通过 segue 传输项目(图像,字符串)
how to transfer item(image,string) by segue
当我点击单元格时。
我想通过segue将所选单元格中的图像和字符串传输。
我尝试使用 "func prepareforsegue".
而且我不知道怎么写。
请帮我。
这是表视图函数:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DiscoverTableViewCell
//set cell
let restaurant = restaurants[indexPath.row]
cell.nameLabel.text = restaurant.objectForKey("name") as? String
cell.typeLabel.text = restaurant.objectForKey("type") as? String
cell.locationLabel.text = restaurant.objectForKey("location") as? String
//set default image
cell.bgImageView.image = nil
//check image in chche or not
if let imageFileUrl = imageCache.objectForKey(restaurant.recordID) as? NSURL {
//get image from cache
print("Get image from cache")
cell.bgImageView.image = UIImage(data: NSData(contentsOfURL: imageFileUrl)!)
}else {
//get data from cloudkit in background
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let fetchRecordsImageOperation = CKFetchRecordsOperation(recordIDs:[restaurant.recordID])
fetchRecordsImageOperation.desiredKeys = ["image"]
fetchRecordsImageOperation.queuePriority = .VeryHigh
fetchRecordsImageOperation.perRecordCompletionBlock = { (record:CKRecord?, recordID:CKRecordID?, error:NSError?) -> Void in
if (error != nil){
print("Fail to get restaurant image: \(error!.localizedDescription)")
return
}
if let restaurantRecord = record {
NSOperationQueue.mainQueue().addOperationWithBlock(){
if let imageAsset = restaurantRecord.objectForKey("image") as? CKAsset {
cell.bgImageView.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!)
// Add the image URL to cache
self.imageCache.setObject(imageAsset.fileURL, forKey: restaurant.recordID)
}
}
}
}
publicDatabase.addOperation(fetchRecordsImageOperation)
}
return cell
}
你可以通过这种方式使用 segue 传递图像
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == showPhotoSegueIdentifier {
if let indexPath = collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let url = photos[indexPath.item]
if let _ = cache.objectForKey(url) {
return true
}
}
return false
}
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == showPhotoSegueIdentifier {
if let indexPath = collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let controller = segue.destinationViewController as! PhotoViewController
let url = photos[indexPath.item]
controller.photo = cache.objectForKey(url) as? UIImage
}
}
}
在第二视图中,您可以使用此代码显示图像
import UIKit
class PhotoViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView?
var photo: UIImage? {
didSet {
imageView?.image = photo
}
}
override func viewDidLoad() {
super.viewDidLoad()
imageView?.image = photo
}
@IBAction func dismiss(sender: AnyObject!) {
dismissViewControllerAnimated(true, completion: nil)
}
}
如果@Anand 的回答不能帮到你,试试这个:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// sender is your Cell, it includes the image and string,
// pass them to next view controller is ok,
// use them in SecondViewController's viewDidLoad() is ok.
let cell = sender as! UITableViewCell
let secondViewController = SecondViewController()
secondViewController.image = cell.imageView?.image
secondViewController.text = cell.textLabel?.text
}
当我点击单元格时。
我想通过segue将所选单元格中的图像和字符串传输。
我尝试使用 "func prepareforsegue".
而且我不知道怎么写。
请帮我。
这是表视图函数:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DiscoverTableViewCell
//set cell
let restaurant = restaurants[indexPath.row]
cell.nameLabel.text = restaurant.objectForKey("name") as? String
cell.typeLabel.text = restaurant.objectForKey("type") as? String
cell.locationLabel.text = restaurant.objectForKey("location") as? String
//set default image
cell.bgImageView.image = nil
//check image in chche or not
if let imageFileUrl = imageCache.objectForKey(restaurant.recordID) as? NSURL {
//get image from cache
print("Get image from cache")
cell.bgImageView.image = UIImage(data: NSData(contentsOfURL: imageFileUrl)!)
}else {
//get data from cloudkit in background
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let fetchRecordsImageOperation = CKFetchRecordsOperation(recordIDs:[restaurant.recordID])
fetchRecordsImageOperation.desiredKeys = ["image"]
fetchRecordsImageOperation.queuePriority = .VeryHigh
fetchRecordsImageOperation.perRecordCompletionBlock = { (record:CKRecord?, recordID:CKRecordID?, error:NSError?) -> Void in
if (error != nil){
print("Fail to get restaurant image: \(error!.localizedDescription)")
return
}
if let restaurantRecord = record {
NSOperationQueue.mainQueue().addOperationWithBlock(){
if let imageAsset = restaurantRecord.objectForKey("image") as? CKAsset {
cell.bgImageView.image = UIImage(data: NSData(contentsOfURL: imageAsset.fileURL)!)
// Add the image URL to cache
self.imageCache.setObject(imageAsset.fileURL, forKey: restaurant.recordID)
}
}
}
}
publicDatabase.addOperation(fetchRecordsImageOperation)
}
return cell
}
你可以通过这种方式使用 segue 传递图像
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == showPhotoSegueIdentifier {
if let indexPath = collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let url = photos[indexPath.item]
if let _ = cache.objectForKey(url) {
return true
}
}
return false
}
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == showPhotoSegueIdentifier {
if let indexPath = collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let controller = segue.destinationViewController as! PhotoViewController
let url = photos[indexPath.item]
controller.photo = cache.objectForKey(url) as? UIImage
}
}
}
在第二视图中,您可以使用此代码显示图像
import UIKit
class PhotoViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView?
var photo: UIImage? {
didSet {
imageView?.image = photo
}
}
override func viewDidLoad() {
super.viewDidLoad()
imageView?.image = photo
}
@IBAction func dismiss(sender: AnyObject!) {
dismissViewControllerAnimated(true, completion: nil)
}
}
如果@Anand 的回答不能帮到你,试试这个:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// sender is your Cell, it includes the image and string,
// pass them to next view controller is ok,
// use them in SecondViewController's viewDidLoad() is ok.
let cell = sender as! UITableViewCell
let secondViewController = SecondViewController()
secondViewController.image = cell.imageView?.image
secondViewController.text = cell.textLabel?.text
}