Last active
April 13, 2017 08:06
-
-
Save lizolni/1265d1a1dc01014583a3bd994d90903e to your computer and use it in GitHub Desktop.
Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class TableViewController: UITableViewController { | |
var photoData : [String] = ["tokyo-09.jpg","Germany.jpg","france.jpg"] | |
var photoName : [String] = ["Tokyo","Germany","France"] | |
var photoDescribe : [String] = ["Tokyo is no1","Tokyo is no2","Tokyo is no3"] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
//設定Navigation bar title | |
self.title = "My Journals" | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
} | |
// MARK: - Table view data source | |
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
// #warning Incomplete implementation, return the number of sections | |
return 1 | |
} | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
// #warning Incomplete implementation, return the number of rows | |
return self.photoData.count | |
} | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = self.tableView.dequeueReusableCellWithIdentifier("photoCell", forIndexPath: indexPath) as! photoCell | |
cell.Photo.image = UIImage(named: self.photoData[indexPath.row]) | |
cell.PhotoTitle.text = self.photoName[indexPath.row] | |
return cell | |
} | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
if segue.identifier == "PhotoView" { | |
if let desination = segue.destinationViewController as? PhotoViewController { | |
//判斷是否從Cell的圖片點選 | |
guard let senderImage = sender as? photoCell else { | |
return | |
} | |
//判斷從哪個圖片indexPath點選 | |
guard let indexpath = tableView?.indexPathForCell(senderImage) as NSIndexPath! else { | |
return | |
} | |
indexpath.row | |
desination.showPhoto.image = UIImage(named : photoData[indexpath.row]) //Error | |
desination.showTitle.text = photoName[indexpath.row] | |
desination.showDescribe?.text = photoDescribe[indexpath.row] | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
desination.showPhoto.image = UIImage(named : photoData[indexpath.row]) can pass
but [ fatal error: unexpectedly found nil while unwrapping an Optional value]