2016-06-06 44 views
0

我想對應我的字典中的「主要」到與每個主要相關的圖片,以便它顯示在單元格中。訪問字符串鍵顯示獲取圖像(斯威夫特)

import Foundation 

class ClassRosterModel { 
    var studentsRoster = [Dictionary<String, String>]() 
    init() { 
     studentsRoster.append(["name": "Kaz, Alex", "number" : "s0834347", "major" : "SE"]) 
     studentsRoster.append(["name": "O'Rore, Ryan", "number" : "s0835357", "major" : "SE"]) 
     studentsRoster.append(["name": "Lote, Lote", "number" : "s0835357", "major" : "SE"]) 
     studentsRoster.append(["name": "Flora, Nico", "number" : "s0748324", "major" : "MA"]) 
    } 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("studentCell", forIndexPath: indexPath) 

    // Configure the cell... 
    cell.textLabel?.text = studentsList[indexPath.row]["name"] 
    cell.detailTextLabel?.text = studentsList[indexPath.row]["number"] 
    print("Student's name: \(studentsList[indexPath.row]["name"])") 
    print("Student's number: \(studentsList[indexPath.row]["number"])") 

    return cell 
+0

目前還不清楚你的問題是什麼。請更新您的問題。 – rmaddy

+0

那麼這有什麼問題? – Alexander

+2

我不會使用字典數組,你應該只使用一個結構體。但無論如何,這應該工作,你已經添加「主要」 –

回答

1

你可以做一個字典,從「大」映射到圖像,例如:

let images = [ 
    "SE": UIImage(named: "se-image.png"), 
    "MD": UIImage(named: "another-image.png") 
] 
let major = studentsList[indexPath.row]["major"] 
if let image = images[major] { 
    cell.imageView?.image = image 
} 

我要對你好,併爲您提供如何編寫良好的銀行代碼一個更好的例子:

enum Major: String { 
    case SE 
    case MA 

    var image: UIImage? { 
     return UIImage(named: self.rawValue) 
    } 
} 
struct Student { 
    let name: String 
    let number: String 
    let major: Major 
} 

class SomeTableViewController: UITableViewController { 
    let students = [ 
     Student(name: "Kaz, Alex", number: "s0834347", major: .SE), 
     Student(name: "O'Rore, Ryan", number: "s0835357", major: .SE), 
     Student(name: "Lote, Lote", number: "s0835357", major: .SE), 
     Student(name: "Flora, Nico", number: "s0748324", major: .MA) 
    ] 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let student = students[indexPath.row] 

     let cell = tableView.dequeueReusableCellWithIdentifier("studentCell", forIndexPath: indexPath) 
     cell.textLabel?.text = student.name 
     cell.detailTextLabel?.text = student.number 
     cell.imageView?.image = student.major.image 
     return cell 
    } 
} 
+0

謝謝你的問題。由於某種原因,故事板不允許我將庫中的圖像添加到細微的單元格中,所以我現在正試圖解決這個問題。 –

+0

我添加了一個代碼示例,以便您可以使您的應用更好,請研究它。 :) –

+0

你是一顆寶石。謝謝 –

0

在你的代碼已經聲明var studentsRoster = [Dictionary<String, String>]()這意味着包含密鑰字符串&值字典的數組。你可能想要做的第一件事是修正,如果你不打算成爲一個字典數組。基本上,[String]是字符串數組的縮寫。 Array<String>是長形式。同樣,Dictionary<String, String>是宣告一個字典,你可以使用的[String:String]

速記所以,你需要啓動的代碼是更可能var studentsRoster = [String:String]()

長表可以添加一個新的密鑰,並設置它的值與studentsRoster["key"] = "Value"

它會對你有很多很好的閱讀以下內容:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID113

+0

RTFM的另一個引人注目的案例 – Alexander

+0

@AMomchilov lol –