如何通过segue传输项目(图像,字符串)

时间:2016-06-04 07:19:54

标签: ios swift segue cloudkit

点击单元格时。
我想通过segue传输所选单元格中的图像和字符串 我尝试使用“func prepareforsegue” 我不知道怎么写。
请帮我。

这是tableview func:

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
}

2 个答案:

答案 0 :(得分:0)

您可以通过这种方式传递图像

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)
    }
}

答案 1 :(得分:0)

如果@Anand的答案无法帮到你,请试试这个:

$(document).ready(function(){
    $(".radioBtn").click(function (){...
});