我是SWIFT的新手,我做了一些示例应用程序。
我唯一无法解决的问题是让我的图像显示在Detail Scene(DetailViewController)上。
在我的MasterViewController文件中,我有这个来获取我的multiData
文件:
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showData" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = self.multiData[indexPath.section][indexPath.row]
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
// Here I get my multiData
func createData() {
var first: [Dictionary<String,String>] = []
var second: [Dictionary<String,String>] = []
dataSections = ["First Data", "Second Data"]
first.append(["name": "someName", "image": "somePngFile", "someData": "someText"])
second.append(["name": "someName", "image": "somePngFile", "someData": "someText"])
multiData = [first, second]
}
DetailViewController:
...
@IBOutlet weak var label: UILabel!
@IBOutlet weak var text: UITextView!
@IBOutlet weak var image: UIImageView!
...
func configureView() {
if let detail = self.detailItem {
if let labelTitle = label {
labelTitle.text = detail["name"] as! String!
}
if let textData = text {
textData.text = detail["someData"] as! String!
}
// This obvious doesn't work
if let imageFile = image {
imageFile.image = detail["image"] as! UIImage!
}
}
}
所以,我的问题是如何从detail["image"]
获取图片?
答案 0 :(得分:1)
我假设detail["image"]
中包含的图像在您的应用程序包中(即:不是来自HTTP
URL的图像)。因此,您应该使用init(named:)
constructor from UIImage
加载图片。这样的事情:
if let myImage = UIImage(named: detail["image"]) {
myImageView.image = myImage
}
修改:
此外,您的代码还可以更好地管理configureView
中的选项和演员:
func configureView() {
if let detail = self.detailItem {
if let labelTitle = detail["name"] as? String {
myLabel.text = labelTitle
}
if let textViewContent = detail["someData"] as? String {
myTextView.text = textViewContent
}
if let myImageName = detail["image"] as? String {
if let myImage = UIImage(named: myImageName) {
myImageView.image = myImage
}
}
}
}