2016-06-07 44 views
-1

我想爲我的字典ex SE,CS,默認情況下不同的專業分配一個圖像,但每次運行該程序時,它崩潰在我身上。我正在使用一個tableview並有一個subtile風格。我應該先分配一些東西給專業嗎?這裏是我的tableviewcontroller指定圖像鍵值(斯威夫特)

import UIKit 
class ClassRosterTableViewController: UITableViewController { 

    var studentsList = [Dictionary<String, String>]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let myStudentRoster = ClassRosterModel() 
     studentsList = myStudentRoster.studentsRoster 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    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"])") 
     print("Student's email: \(studentsList[indexPath.row]["email"])") 

     if let major = studentsList[indexPath.row]["major"] { 
      switch (major) { 
      case "SE": 

       cell.imageView?.image = UIImage(named:"SE")! 

      case "CS": 

       cell.imageView?.image = UIImage(named:"CS")! 
      default: 
       cell.imageView?.image = UIImage(named:"default")! 
      } 
     } 

     return cell 
    } 


//here is my separate class for the studentslist 

import Foundation 

class ClassRosterModel { 

    var studentsRoster = [Dictionary<String, String>]() 
    init() { 

     studentsRoster.append(["name": "Kaczynski, Alex", "major" : "SE", "email" : "s\(rand1)@monmouth.edu", "currentTerm" : "Spring", "numberOfCredits" : "\(rand2)" ]) 
     studentsRoster.append(["name": "O'Rore, Ryan", "major" : "SE", "email" : "s\(rand1)@monmouth.edu", "currentTerm" : "Fall", "numberOfCredits" : "\(rand2)" ]) 
    } 
} 
+1

什麼行崩潰,什麼是故障控制檯消息?你的代碼中有很多驚歎號;其中每一個都意味着「請讓我崩潰」。你不會感到驚訝,如果它_does_崩潰;你自找的。 – matt

+0

非常感謝,我不知道他們對代碼有這樣的影響,刪除它們,然後刪除中斷點就是實際存在的問題,並停止了我的代碼的工作。 –

回答

0

您是力量展開這些圖像,如果它們不存在在你的應用程序的包就會死機。 UIImageViewimage屬性是可選的,所以它的精細分配這些圖像沒有解開他們:

if let major = studentsList[indexPath.row]["major"] { 
     switch (major) { 
     case "SE": 
      cell.imageView?.image = UIImage(named:"SE") 
     case "CS": 
      cell.imageView?.image = UIImage(named:"CS") 
     default: 
      cell.imageView?.image = UIImage(named:"default") 
     } 
    } 

注:如果圖像是不存在於你的包,你的ImageView的將是空的,但至少它贏得了「 t崩潰。

+0

謝謝你是這個問題。刪除破點後,它的工作完美無瑕。 –