试图调用图像数组

时间:2018-03-16 17:47:09

标签: ios uiimage

我有一个需要根据被点击的单元格调用的图像数组。例如,假设您有五个单元格,每个单元格都有自己的特定单元格信息。当您点击第一个单元格时,它会打开一个新页面,图像会显示该数组中的单元格以及随附的信息。现在,当您点击单元格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
}
}

1 个答案:

答案 0 :(得分:0)

#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()
}