目标控制器
class CharDetailsViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var genderLabel: UILabel!
@IBOutlet weak var houseLabel: UILabel!
@IBOutlet weak var ancestryLabel: UILabel!
var image = UIImage()
var name = String()
var gender = String()
var house = String()
var ancestry = String()
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = image
nameLabel.text! = name
houseLabel.text! = house
// Do any additional setup after loading the view.
}
源控制器
class CharacterTableViewController: UITableViewController {
var charactersData = [Character]()
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
func loadData()
{
DispatchQueue.main.async {
Alamofire.request("http://hp-api.herokuapp.com/api/characters").responseJSON(completionHandler: {
(response) in
switch response.result
{
case.success(let value):
let json = JSON(value)
print(json)
json.array?.forEach({
(character) in
let character = Character(name: character["name"].stringValue, house:character["house"].stringValue,image:character["image"].stringValue, gender: character["gender"].stringValue, ancestry: character["ancestry"].stringValue)
self.charactersData.append(character)
})
self.tableView.reloadData()
case.failure(let error):
print(error.localizedDescription)
}
})
}
}
override func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return charactersData.count
}
override func tableView( tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CharTableViewCell
cell.nameLabel.text = "Name: " + charactersData[indexPath.row].name
cell.houseLabel.text = "House: " + charactersData[indexPath.row].house
if let imageURL = URL(string: self.charactersData[indexPath.row].image) {
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
cell.charImageView.image = image
}
}
}
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let hpc = storyboard?.instantiateViewController(withIdentifier: "CharDetails") as? CharDetailsViewController
hpc?.image = UIImage(named: charactersData[indexPath.row].image)! //Error found here
hpc?.name = charactersData[indexPath.row].name
hpc?.house = charactersData[indexPath.row].house
self.navigationController?.pushViewController(hpc!, animated: true)
}
class Character
{
var name : String
var house : String
var image : String
init(name: String, house: String, image: String)
{
self.name = name
self.house = house
self.image = image
}
}
我在展开一个可选值图像以将该图像传递给另一个控制器时得到了意外发现的nil。我已经检查了网点,它们非常好。如上所示,这些图像是从JSON格式的url数组中获取的,它们在表视图上运行良好,并在表视图上可以看到,但未在详细信息视图页面上显示。请帮助我。
答案 0 :(得分:-1)
您正试图强行打开UIImage,这是无效的。当图片出现在套装中并且您要从那里展示时,使用R.drawable.asdf
方法。
UIImage(named:)
在上一行,您正在使用UIImage(named :)!这意味着,您必须对其进行解包,这会导致在解包可选值时意外发现nil崩溃
解决方案 您可以传递与名称和房屋相同的图像名称,
1)更新您的
hpc?.image = UIImage(named: charactersData[indexPath.row].image)! //Error found here
2)在didSelectRow中,更新代码以传递 imageUrlString
class CharDetailsViewController: UIViewController {
// Add this property to hold url string for image.
var imageUrlString = String()
}
2)将其放在CharDetailsViewController的viewDidLoad方法中
hpc?.imageUrlString = charactersData[indexPath.row].image
希望有帮助