Advertisement
ZTCYING

Lab- People Information

May 4th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 10.09 KB | None | 0 0
  1. //
  2. //  Person.swift
  3. //  PersonInformation
  4. //
  5. //  Created by Sabin Tabirca on 09/02/2024.
  6. //
  7.  
  8. import Foundation
  9.  
  10. class Person{
  11.    
  12.     // class properties
  13.     var name, address, phone, image, url : String
  14.    
  15.     // class init-s
  16.     init(name: String, address: String, phone: String, image: String, url: String) {
  17.         self.name = name
  18.         self.address = address
  19.         self.phone = phone
  20.         self.image = image
  21.         self.url = url
  22.     }
  23.     init() {
  24.         self.name = "John Doe"
  25.         self.address = "no address"
  26.         self.phone = "no phone"
  27.         self.image = "doe.jpg"
  28.         self.url = "na"
  29.     }
  30.    
  31.     // class methods
  32.    
  33. }
  34.  
  35. //
  36. //  People.swift
  37. //  PersonInformation
  38. //
  39. //  Created by Sabin Tabirca on 09/02/2024.
  40. //
  41.  
  42. import Foundation
  43. class People{
  44.    
  45.     var peopleData : [Person]
  46.    
  47.     init(){
  48.        
  49.         peopleData =
  50.         [
  51.             Person(name: "Sabin Tabirca", address: "WGB, UCC, Cork", phone: "12345678", image: "sabin.jpeg", url: "http://www.cs.ucc.ie"),
  52.             Person(name: "Sabina Tabirca", address: "CUMH, UCC, Cork", phone: "12345678", image: "sabin.jpeg", url: "http://www.cs.ucc.ie"),
  53.             Person(name: "John Tabirca", address: "Loyds,  Cork", phone: "12345678", image: "sabin.jpeg", url: "http://www.cs.ucc.ie"),
  54.             Person(name: "Tany Tabirca", address: "WGB, UCC, Cork", phone: "12345678", image: "sabin.jpeg", url: "http://www.cs.ucc.ie"),
  55.             Person(name: "Sabinus Tabirca", address: "WGB, UCC, Cork", phone: "12345678", image: "sabin.jpeg", url: "http://www.cs.ucc.ie")
  56.            
  57.         ]
  58.     }
  59.    
  60.     func getPerson(index:Int)-> Person{
  61.         return peopleData[index]
  62.     }
  63.    
  64.     func getCount()->Int{
  65.         return peopleData.count
  66.     }
  67. }
  68.  
  69. //
  70. //  PersonViewController.swift
  71. //  PersonInformation
  72. //
  73. //  Created by Sabin Tabirca on 09/02/2024.
  74. //
  75.  
  76. import UIKit
  77.  
  78. class PersonViewController: UIViewController {
  79.     // outlets and actions
  80.  
  81.     @IBOutlet weak var personImageView: UIImageView!
  82.     @IBOutlet weak var personNameLabel: UILabel!
  83.    
  84.    
  85.    
  86.    
  87.     // vars and methods
  88.     var personData : Person!  
  89.  
  90.     override func viewDidLoad() {
  91.         super.viewDidLoad()
  92.  
  93.         // Do any additional setup after loading the view.
  94.        
  95.         // fill the label and image view with data
  96.         personNameLabel.text = personData.name
  97.         personImageView.image = UIImage(named: personData.image)
  98.     }
  99.    
  100.     // MARK: - Navigation
  101.  
  102.     // 在基于故事板的应用程序中,您通常需要在导航之前做一些准备工作
  103.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  104.         if segue.identifier == "segue1"{
  105.             // 使用 segue.destination 获取新的视图控制器。
  106.             let destination = segue.destination as! DetailsViewController
  107.            
  108.             // 将选定的对象传递给新的视图控制器。
  109.             destination.personData = self.personData
  110.         }
  111.     }
  112.    
  113.  
  114. }
  115.  
  116. //
  117. //  DetailsViewController.swift
  118. //  PersonInformation
  119. //
  120. //  Created by Sabin Tabirca on 09/02/2024.
  121. //
  122.  
  123. import UIKit
  124.  
  125. class DetailsViewController: UIViewController {
  126.     // outlets
  127.    
  128.    
  129.     @IBOutlet weak var nameLabel: UILabel!
  130.     @IBOutlet weak var phoneLabel: UILabel!
  131.     @IBOutlet weak var addressLabel: UILabel!
  132.     @IBOutlet weak var imageLabel: UILabel!
  133.     @IBOutlet weak var urlLabel: UILabel!
  134.    
  135.     // vars and methods
  136.     var personData : Person!
  137.    
  138.    
  139.     override func viewDidLoad() {
  140.         super.viewDidLoad()
  141.  
  142.         // Do any additional setup after loading the view.
  143.         nameLabel.text = personData.name
  144.         phoneLabel.text = personData.phone
  145.         addressLabel.text = personData.address
  146.         urlLabel.text = personData.url
  147.         imageLabel.text = personData.image
  148.     }
  149.    
  150.  
  151.     /*
  152.     // MARK: - Navigation
  153.  
  154.     // In a storyboard-based application, you will often want to do a little preparation before navigation
  155.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  156.         // Get the new view controller using segue.destination.
  157.         // Pass the selected object to the new view controller.
  158.     }
  159.     */
  160.  
  161. }
  162.  
  163. //
  164. //  PeopleTableViewController.swift
  165. //  PersonInformation
  166. //
  167. //  Created by Sabin Tabirca on 16/02/2024.
  168. //
  169.  
  170. import UIKit
  171.  
  172. class PeopleTableViewController: UITableViewController {
  173.    
  174.     // declare data
  175.     var peopleData : People!
  176.  
  177.     override func viewDidLoad() {
  178.         super.viewDidLoad()
  179.         // make data
  180.         peopleData = People()
  181.  
  182.     }
  183.  
  184.     // MARK: - Table view data source
  185.  
  186.     override func numberOfSections(in tableView: UITableView) -> Int {
  187.         // #warning Incomplete implementation, return the number of sections#warning 执行不完整,返回节数
  188.         return 1
  189.     }
  190.  
  191.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  192.         // #warning Incomplete implementation, return the number of rows#warning 执行不完整,返回行数
  193.         return peopleData.getCount()
  194.     }
  195.  
  196.    
  197.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  198.         let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  199.  
  200.         // Configure the cell...
  201.         let personData = peopleData.getPerson(index: indexPath.row)
  202.         cell.textLabel?.text = personData.name
  203.         cell.detailTextLabel?.text = personData.phone
  204.         cell.imageView?.image = UIImage(named: personData.image)
  205.  
  206.         return cell
  207.     }
  208.    
  209.  
  210.     /*
  211.     // Override to support conditional editing of the table view.覆盖以支持表视图的条件编辑。
  212.     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  213.         // Return false if you do not want the specified item to be editable.如果您不希望指定的项目可编辑,则返回 false。
  214.         return true
  215.     }
  216.     */
  217.  
  218.     /*
  219.     // Override to support editing the table view.覆盖以支持编辑表视图。
  220.     override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  221.         if editingStyle == .delete {
  222.             // Delete the row from the data source
  223.             tableView.deleteRows(at: [indexPath], with: .fade)
  224.         } else if editingStyle == .insert {
  225.             // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view创建适当类的新实例,将其插入数组,并向表视图添加新行
  226.         }    
  227.     }
  228.     */
  229.  
  230.     /*
  231.     // Override to support rearranging the table view.重写以支持重新排列表视图。
  232.     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
  233.  
  234.     }
  235.     */
  236.  
  237.     /*
  238.     // Override to support conditional rearranging of the table view.重写以支持表视图的条件重新排列。
  239.     override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
  240.         // Return false if you do not want the item to be re-orderable.如果您不希望该商品可重新订购,请返回 false。
  241.         return true
  242.     }
  243.     */
  244.  
  245.    
  246.     // MARK: - Navigation
  247.  
  248.     // In a storyboard-based application, you will often want to do a little preparation before navigation
  249.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  250.         if segue.identifier == "segue2"{
  251.             // Get indexPath and data associated with获取indexPath和关联的数据
  252.             let indexPath = tableView.indexPath(for: sender as!UITableViewCell)
  253.             let personData = peopleData.getPerson(index: indexPath!.row)
  254.            
  255.             // Get the new view controller using segue.destination.使用 segue.destination 获取新的视图控制器。
  256.             let destination = segue.destination as! PersonViewController
  257.            
  258.             // Pass the selected object to the new view controller.将选定的对象传递给新的视图控制器。
  259.             destination.personData = personData
  260.         }
  261.     }
  262.    
  263.  
  264. }
  265.  
  266. //
  267. //  AppDelegate.swift
  268. //  PersonInformation
  269. //
  270. //  Created by Sabin Tabirca on 09/02/2024.
  271. //
  272.  
  273. import UIKit
  274.  
  275. @main
  276. class AppDelegate: UIResponder, UIApplicationDelegate {
  277.  
  278.  
  279.  
  280.     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  281.         // Override point for customization after application launch.应用程序启动后自定义的覆盖点。
  282.         return true
  283.     }
  284.  
  285.     // MARK: UISceneSession Lifecycle
  286.  
  287.     func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
  288.         // Called when a new scene session is being created.创建新场景会话时调用。
  289.         // Use this method to select a configuration to create the new scene with.使用此方法选择用于创建新场景的配置。
  290.         return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  291.     }
  292.  
  293.     func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  294.     }
  295.  
  296.  
  297. }
  298.  
  299. //
  300. //  SceneDelegate.swift
  301. //  PersonInformation
  302. //
  303. //  Created by Sabin Tabirca on 09/02/2024.
  304. //
  305.  
  306. import UIKit
  307.  
  308. class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  309.  
  310.     var window: UIWindow?
  311.  
  312.  
  313.     func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
  314.      
  315.         guard let _ = (scene as? UIWindowScene) else { return }
  316.     }
  317.  
  318.     func sceneDidDisconnect(_ scene: UIScene) {
  319.        
  320.     }
  321.  
  322.     func sceneDidBecomeActive(_ scene: UIScene) {
  323.        
  324.     }
  325.  
  326.     func sceneWillResignActive(_ scene: UIScene) {
  327.        
  328.     }
  329.  
  330.     func sceneWillEnterForeground(_ scene: UIScene) {
  331.        
  332.     }
  333.  
  334.     func sceneDidEnterBackground(_ scene: UIScene) {
  335.        
  336.     }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement