试图调用图像数组
Trying to call an array of Images
我有一个图像数组,需要根据点击的单元格调用这些图像。例如,假设您有五个单元格,每个单元格都有属于该特定单元格的自己的信息。当您点击第一个单元格时,它会打开一个新页面,并显示数组中该单元格的图像以及随之而来的信息。现在,当您点击单元格 2 时,将填充图像以及专门用于单元格 2 的信息。如何从属于被点击的单元格的数组中调用图像?我知道如何用字符串来做,但我似乎无法弄清楚如何用图像来做。当我尝试以与处理字符串相同的方式进行操作时,我得到的只是错误。
这是代码的一部分:
import UIKit
class FacultyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
var facultyMemeber: [String] = ["Ron Cervantes", "Darice Corey-Gilbert", "Devan Shepard", "Tom Sinclair"]
var image: UIImage = [#imageLiteral(resourceName: "rcervantas"), #imageLiteral(resourceName: "dcorey"), #imageLiteral(resourceName: "dshepherd"), #imageLiteral(resourceName: "tsinclair")]
var bio: [String] = ["Ron is a tech billionaire who finds fulfillment in teaching!", "Darice oversees all aspects of Web design and development for Yale College. Coordinates planning and implementation of new and existing Web projects for Yale College clients. Establishes web design and functionality standards. Works with the Director of IT Planning and Coordination to provide strategic Web solutions for Yale College. The Director strategically plans and implements Web development at Yale College. The Director collaborates with the Dean and upper-level management on matters of Web strategy, and develops and maintains relations with client offices. She supervises the activities of YCWS staff, especially in regard to day-to-day support of client operations. The Director anticipates imminent advances in Web technology, manages contracts with outside design and programming vendors including ITS; and oversees YCWS's budget, administrative and office practices, hiring, and human resource contacts.", "CEO, Chief Technical Officer - XMaLpha Technologies, AOPA Outstanding Flight Instructor of the Year, 2012", "Owner and Senior Consultant at Cynomys Consulting, Adjunct Faculty at Westwood College; formerly Program Chair - Information Technology at Westwood College, Senior Consultant, I.T. Architecture at KPMG CT, Unix Administrator at Bell Labs Lucent"]
var education: [String] = ["MS Management Information Systems | Keller Graduate School of Management 2007 \nBA Business w/concentration in MIS | University of Wisconsin-Parkside 1999", "MBA Information Technology and E-Business | Fairfield University 2002 \nBS Information Systems | Fairfield University 1998", "MS Information Technology | Capella University \nBA Psychology | Wilfrid Laurier University", "MS Computer Science | Loyola University, Chicago, Illinois \nBS Computer Science | Loyola University, Chicago, Illinois"]
var selectedRow: Int = -1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return facultyMemeber.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let faculty:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "faculty")!
faculty.textLabel?.text = facultyMemeber[indexPath.row]
return faculty
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "Faculty", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailView: DetailFacultyViewController = segue.destination as! DetailFacultyViewController
selectedRow = (table.indexPathForSelectedRow?.row)!
detailView.facultyNameString = facultyMemeber[selectedRow]
detailView.bioString = bio[selectedRow]
detailView.detailImage = image[selectedRow]
detailView.educationString = education[selectedRow]
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
它调用的另一部分是:
导入 UIKit
class DetailFacultyViewController: UIViewController {
@IBOutlet weak var facultyName: UILabel!
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var bio: UILabel!
@IBOutlet weak var education: UILabel!
var facultyNameString: String!
var detailImage: String!
var bioString: String!
var educationString: String!
override func viewWillAppear(_ animated: Bool) {
super.viewDidLoad()
setFacultyName()
setBio()
setEducation()
// Do any additional setup after loading the view.
}
func setFacultyName () {
facultyName.text = facultyNameString }
func setBio() {
bio.text = bioString
}
func setEducation() {
education.text = educationString
}
func setImage() {
image.image = detailImage
}
}
#imageLiteral(resourceName:)
给了我们一个 UIImage
所以在你的情况下:-
detailImage
也应该是 UIImage
而不是 String
变化:
var detailImage: String!
到
var detailImage: UIImage!
然后继续你正在做的事情:
image.image = detailImage
编辑:
同时更改:
var image: UIImage = [#imageLiteral(resourceName: "rcervantas"),...]
到
//should be an array of images as per your design
var image: [UIImage] = [#imageLiteral(resourceName: "rcervantas"),...]
最后调用setImage()
override func viewWillAppear(_ animated: Bool) {
//...
setImage()
}
我有一个图像数组,需要根据点击的单元格调用这些图像。例如,假设您有五个单元格,每个单元格都有属于该特定单元格的自己的信息。当您点击第一个单元格时,它会打开一个新页面,并显示数组中该单元格的图像以及随之而来的信息。现在,当您点击单元格 2 时,将填充图像以及专门用于单元格 2 的信息。如何从属于被点击的单元格的数组中调用图像?我知道如何用字符串来做,但我似乎无法弄清楚如何用图像来做。当我尝试以与处理字符串相同的方式进行操作时,我得到的只是错误。
这是代码的一部分:
import UIKit
class FacultyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
var facultyMemeber: [String] = ["Ron Cervantes", "Darice Corey-Gilbert", "Devan Shepard", "Tom Sinclair"]
var image: UIImage = [#imageLiteral(resourceName: "rcervantas"), #imageLiteral(resourceName: "dcorey"), #imageLiteral(resourceName: "dshepherd"), #imageLiteral(resourceName: "tsinclair")]
var bio: [String] = ["Ron is a tech billionaire who finds fulfillment in teaching!", "Darice oversees all aspects of Web design and development for Yale College. Coordinates planning and implementation of new and existing Web projects for Yale College clients. Establishes web design and functionality standards. Works with the Director of IT Planning and Coordination to provide strategic Web solutions for Yale College. The Director strategically plans and implements Web development at Yale College. The Director collaborates with the Dean and upper-level management on matters of Web strategy, and develops and maintains relations with client offices. She supervises the activities of YCWS staff, especially in regard to day-to-day support of client operations. The Director anticipates imminent advances in Web technology, manages contracts with outside design and programming vendors including ITS; and oversees YCWS's budget, administrative and office practices, hiring, and human resource contacts.", "CEO, Chief Technical Officer - XMaLpha Technologies, AOPA Outstanding Flight Instructor of the Year, 2012", "Owner and Senior Consultant at Cynomys Consulting, Adjunct Faculty at Westwood College; formerly Program Chair - Information Technology at Westwood College, Senior Consultant, I.T. Architecture at KPMG CT, Unix Administrator at Bell Labs Lucent"]
var education: [String] = ["MS Management Information Systems | Keller Graduate School of Management 2007 \nBA Business w/concentration in MIS | University of Wisconsin-Parkside 1999", "MBA Information Technology and E-Business | Fairfield University 2002 \nBS Information Systems | Fairfield University 1998", "MS Information Technology | Capella University \nBA Psychology | Wilfrid Laurier University", "MS Computer Science | Loyola University, Chicago, Illinois \nBS Computer Science | Loyola University, Chicago, Illinois"]
var selectedRow: Int = -1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return facultyMemeber.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let faculty:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "faculty")!
faculty.textLabel?.text = facultyMemeber[indexPath.row]
return faculty
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "Faculty", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailView: DetailFacultyViewController = segue.destination as! DetailFacultyViewController
selectedRow = (table.indexPathForSelectedRow?.row)!
detailView.facultyNameString = facultyMemeber[selectedRow]
detailView.bioString = bio[selectedRow]
detailView.detailImage = image[selectedRow]
detailView.educationString = education[selectedRow]
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
它调用的另一部分是: 导入 UIKit
class DetailFacultyViewController: UIViewController {
@IBOutlet weak var facultyName: UILabel!
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var bio: UILabel!
@IBOutlet weak var education: UILabel!
var facultyNameString: String!
var detailImage: String!
var bioString: String!
var educationString: String!
override func viewWillAppear(_ animated: Bool) {
super.viewDidLoad()
setFacultyName()
setBio()
setEducation()
// Do any additional setup after loading the view.
}
func setFacultyName () {
facultyName.text = facultyNameString }
func setBio() {
bio.text = bioString
}
func setEducation() {
education.text = educationString
}
func setImage() {
image.image = detailImage
}
}
#imageLiteral(resourceName:)
给了我们一个 UIImage
所以在你的情况下:-
detailImage
也应该是 UIImage
而不是 String
变化:
var detailImage: String!
到
var detailImage: UIImage!
然后继续你正在做的事情:
image.image = detailImage
编辑:
同时更改:
var image: UIImage = [#imageLiteral(resourceName: "rcervantas"),...]
到
//should be an array of images as per your design
var image: [UIImage] = [#imageLiteral(resourceName: "rcervantas"),...]
最后调用setImage()
override func viewWillAppear(_ animated: Bool) {
//...
setImage()
}