我已成功将图片解析为cellForRowAt
,但现在需要在didSelectRowAt
中为ResultsViewController
的食谱显示该图片,并遇到问题。
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
}
...
}
我可以调用downloadImage
中的cellForRowAt
功能,但无法找到将该图像推送到下一个视图控制器的方法。 didSelectRowAt
中有注释代码,它返回错误:
"无法指定类型的值'()'输入' UIImage'"
非常感谢帮助。
答案 0 :(得分:0)
将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