我已经成功地将图像解析为cellForRowAt
,但是现在需要为didSelectRowAt
的ResultsViewController
中的配方将其显示为downloadImage
,并且遇到了麻烦。
RecipeTableViewController.swift
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! RecipeTableViewCell
if let item = tableViewDataSource[indexPath.item] as? Recipe {
if isSearching {
cell.item = filteredData[indexPath.row]
cell.recipeImage.downloadImage(from: (self.filteredData[indexPath.item].image))
} else {
cell.item = item
cell.recipeImage.downloadImage(from: (self.tableViewDataSource[indexPath.item].image))
}
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let resultsVC = Storyboard.instantiateViewController(withIdentifier: "ResultsViewController") as! ResultsViewController
// Information to be passed to ResultsViewController
if (tableViewDataSource[indexPath.item] as? Recipe) != nil {
if isSearching {
resultsVC.getTitle = filteredData[indexPath.row].title
resultsVC.getIngredients = filteredData[indexPath.row].ingredients
// Parse Image
} else {
resultsVC.getTitle = tableViewDataSource[indexPath.row].title
resultsVC.getIngredients = tableViewDataSource[indexPath.row].ingredients
// Parse Image
//resultsVC.getImage = resultsVC.imageDisplay.downloadImage(from (self.tableViewDataSource[indexPath.item].image))
}
}
// Push to next view
self.navigationController?.pushViewController(resultsVC, animated: true)
}
extension UIImageView {
func downloadImage(from url: String) {
let urlRequest = URLRequest(url: URL(string: url)!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.sync {
self.image = UIImage(data: data!)
}
}
task.resume()
}
}
ResultsViewController.swift
class ResultsViewController: UIViewController {
var getTitle = String()
var getIngredients = String()
var getImage = String()
@IBOutlet weak var recipeDisplay: UILabel!
@IBOutlet weak var ingredientsDisplay: UITextView!
@IBOutlet weak var imageDisplay: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
recipeDisplay.text! = getTitle
ingredientsDisplay.text! = getIngredients
imageDisplay.image! = getImage
}
...
}
我可以在
cellForRowAt
中调用didSelectRowAt
函数,但无法找出将图像推送到下一个视图控制器的方法。 中有注释的代码,该代码返回错误:“无法将类型'()'的值分配给类型'UIImage'”
感谢帮助。
最佳答案
将didSelectRowAt
替换为以下内容,
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let resultsVC = Storyboard.instantiateViewController(withIdentifier: "ResultsViewController") as! ResultsViewController
// Information to be passed to ResultsViewController
if (tableViewDataSource[indexPath.item] as? Recipe) != nil {
if isSearching {
resultsVC.getTitle = filteredData[indexPath.row].title
resultsVC.getIngredients = filteredData[indexPath.row].ingredients
// Parse Image
resultsVC.getImage = filteredData[indexPath.item].image
} else {
resultsVC.getTitle = tableViewDataSource[indexPath.row].title
resultsVC.getIngredients = tableViewDataSource[indexPath.row].ingredients
// Parse Image
resultsVC.getImage = self.tableViewDataSource[indexPath.item].image
}
}
// Push to next view
self.navigationController?.pushViewController(resultsVC, animated: true)
}
然后像这样更新
ResultsViewController
class ResultsViewController: UIViewController {
var getTitle = String()
var getIngredients = String()
var getImage = String()
@IBOutlet weak var recipeDisplay: UILabel!
@IBOutlet weak var ingredientsDisplay: UITextView!
@IBOutlet weak var imageDisplay: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
recipeDisplay.text = getTitle
ingredientsDisplay.text = getIngredients
imageDisplay.downloadImage(from: getImage)
}
...
}
另外,确保在
IBOutlets
中连接了storyboard