目标控制器
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数组中获取,它们在表视图上正常工作并在表视图中看到,但它们没有显示在详细信息视图页面上。请帮助我。
你试图强行打开UIImage,这是零。当图像存在于包内并且您想从那里显示时,使用UIImage(named:)
方法。
hpc?.image = UIImage(named: charactersData[indexPath.row].image)! //Error found here
在上面的行中,您正在使用UIImage(名为:)!这意味着,你是强行打开它,导致在意外发现nil时崩溃,同时解开一个可选值
解决方案您可以传递与名称和房屋相同的图像名称,
1)更新你的
class CharDetailsViewController: UIViewController {
// Add this property to hold url string for image.
var imageUrlString = String()
}
2)在didSelectRow中,更新代码以传递imageUrlString
hpc?.imageUrlString = charactersData[indexPath.row].image
2)将它放在CharDetailsViewController的viewDidLoad方法中
if let imageURL = URL(string: imageUrlString) {
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
self.imageView.image = image
}
}
}
}
希望能帮助到你