第十天:SwiftGoodAsOldPhones

 

参考链接:https://github.com/soapyigu/Swift-30-Projects

 

 1 import UIKit
 2 
 3 class Product: NSObject {
 4     var name: String?
 5     var cellImageName: String?
 6     var fullscreenImageName: String?
 7     
 8     init(name: String, cellImageName: String, fullscreenImageName: String) {
 9         self.name = name
10         self.cellImageName = cellImageName
11         self.fullscreenImageName = fullscreenImageName
12     }
13 }

 

  1 import UIKit
  2 
  3 class ProductsTableViewController: UITableViewController {
  4 
  5     fileprivate var products: [Product]?
  6     fileprivate let identifier = "productCell"
  7     
  8     override func viewDidLoad() {
  9         super.viewDidLoad()
 10 
 11         // Uncomment the following line to preserve selection between presentations
 12         // self.clearsSelectionOnViewWillAppear = false
 13 
 14         // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
 15         // self.navigationItem.rightBarButtonItem = self.editButtonItem
 16         
 17         products = [
 18             Product.init(name: "1907 Wall Set", cellImageName: "image-cell1", fullscreenImageName: "phone-fullscreen1"),
 19             Product.init(name: "1921 Dial Phone", cellImageName: "image-cell2", fullscreenImageName: "phone-fullscreen2"),
 20             Product.init(name: "1937 Desk Set", cellImageName: "image-cell3", fullscreenImageName: "phone-fullscreen3"),
 21             Product.init(name: "1984 Moto Portable", cellImageName: "image-cell4", fullscreenImageName: "phone-fullscreen4")
 22         ]
 23     }
 24 
 25     override func didReceiveMemoryWarning() {
 26         super.didReceiveMemoryWarning()
 27         // Dispose of any resources that can be recreated.
 28     }
 29 
 30     // MARK: - Table view data source
 31 
 32     override func numberOfSections(in tableView: UITableView) -> Int {
 33         // #warning Incomplete implementation, return the number of sections
 34         return 1
 35     }
 36 
 37     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 38         // #warning Incomplete implementation, return the number of rows
 39         return products?.count ?? 0
 40     }
 41 
 42     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 43         let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
 44 
 45         guard let products = products else { return cell }
 46 
 47         // Configure the cell...
 48         cell.textLabel?.text = products[(indexPath as NSIndexPath).row].name
 49         
 50         if let imageName = products[(indexPath as NSIndexPath).row].cellImageName {
 51             cell.imageView?.image = UIImage.init(named: imageName)
 52         }
 53 
 54         return cell
 55     }
 56 
 57     /*
 58     // Override to support conditional editing of the table view.
 59     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
 60         // Return false if you do not want the specified item to be editable.
 61         return true
 62     }
 63     */
 64 
 65     /*
 66     // Override to support editing the table view.
 67     override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
 68         if editingStyle == .delete {
 69             // Delete the row from the data source
 70             tableView.deleteRows(at: [indexPath], with: .fade)
 71         } else if editingStyle == .insert {
 72             // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 73         }    
 74     }
 75     */
 76 
 77     /*
 78     // Override to support rearranging the table view.
 79     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
 80 
 81     }
 82     */
 83 
 84     /*
 85     // Override to support conditional rearranging of the table view.
 86     override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
 87         // Return false if you do not want the item to be re-orderable.
 88         return true
 89     }
 90     */
 91 
 92     
 93     // MARK: - Navigation
 94 
 95     // In a storyboard-based application, you will often want to do a little preparation before navigation
 96     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 97         // Get the new view controller using segue.destinationViewController.
 98         // Pass the selected object to the new view controller.
 99         if segue.identifier == "showProduct" {
100             if let cell = sender as? UITableViewCell, let indexPath = tableView.indexPath(for: cell), let productVC = segue.destination as? ProductViewController {
101                 productVC.product = products?[(indexPath as NSIndexPath).row]
102             }
103         }
104     }
105     
106 
107 }

 

 1 import UIKit
 2 
 3 class ProductViewController: UIViewController {
 4     
 5     @IBOutlet var productImageView: UIImageView!
 6     @IBOutlet var productNameLabel: UILabel!
 7     
 8     var product: Product?
 9 
10     override func viewDidLoad() {
11         super.viewDidLoad()
12 
13         // Do any additional setup after loading the view.
14         
15         productNameLabel.text = product?.name
16         
17         if let imageName = product?.fullscreenImageName {
18             productImageView.image = UIImage.init(named: imageName)
19         }
20     }
21 
22     @IBAction func addToCartButtonDidTap(_ sender: AnyObject) {
23         print("Add to cart successfully")
24     }
25     
26     override func didReceiveMemoryWarning() {
27         super.didReceiveMemoryWarning()
28         // Dispose of any resources that can be recreated.
29     }
30     
31 
32     /*
33     // MARK: - Navigation
34 
35     // In a storyboard-based application, you will often want to do a little preparation before navigation
36     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
37         // Get the new view controller using segue.destinationViewController.
38         // Pass the selected object to the new view controller.
39     }
40     */
41 
42 }

 

posted @ 2018-02-27 07:51  鳄鱼不怕牙医不怕  阅读(170)  评论(0编辑  收藏  举报