gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:22:26 25 4
gpt4 key购买 nike

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

这是表格 View 函数:

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
}

最佳答案

如果@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
}

关于ios - 如何通过segue传输项目(图像,字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37627556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com