【ios开发/Xcode】使用UITableView完成学生信息及成绩的显示
生活随笔
收集整理的這篇文章主要介紹了
【ios开发/Xcode】使用UITableView完成学生信息及成绩的显示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【ios開發/Xcode】使用UITableView完成學生信息及成績的顯示
- 設計思想
- 實現效果
- 源代碼
設計思想
首先創建所有頁面的故事版,包括,登錄、注冊與成績頁面
接著設置故事版的關聯代碼,如下圖所示為用戶名與密碼UITextField的關聯
再進行一些空間的格式類型等設置,如下圖所示為TableViewCell單元格屬性的設置
最后在增加增刪改查的代碼,完成設計
實現效果
注冊賬號:lct,密碼:lct,登錄系統
進入系統,看到學生成績信息,進行編輯操作/刪除操作/增加操作
源代碼
注:@開頭的這些代碼都是需要關聯控鍵,都需要自行在故事板中(Storyboards)進行關聯
登錄界面
//登錄界面
import UIKitclass LoginViewController : UIViewController{ @IBOutlet weak var userId: UITextField!@IBOutlet weak var password: UITextField!override func viewDidLoad() {super.viewDidLoad()NotificationCenter.default.addObserver(self, selector: #selector(autoFillInfomation(_:)), name: Notification.Name(rawValue: "RegisterNotification"), object: nil)}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()NotificationCenter.default.removeObserver(self)}@objc func autoFillInfomation(_ notification: Notification) {let userInfo = notification.userInfo!let userId1 = userInfo["userId"] as! Stringlet password1 = userInfo["password"] as! StringuserId.text = userId1password.text = password1// users[userId1] = password1}
}
注冊界面
// 注冊界面
import UIKit
class RegisterViewController : UIViewController{@IBOutlet weak var userId: UITextField!@IBOutlet weak var password: UITextField!override func viewDidLoad() {super.viewDidLoad()}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}@IBAction func back(_ sender: Any) {self.dismiss(animated: true, completion: nil)}@IBAction func register(_ sender: Any) {self.dismiss(animated: true) { () -> Void inlet userInfo = ["userId" :self.userId.text!, "password": self.password.text!]NotificationCenter.default.post(name: Notification.Name(rawValue: "RegisterNotification"),object:nil,userInfo: userInfo)} }
}
定義數據字段
//定義數據字段
import UIKit
class Student:NSObject{var name:Stringvar score:Intinit(name:String,score:Int){self.name=nameself.score=scoresuper.init()}
}
定義增加、刪除、移動等操作
//定義增加、刪除、移動等操作
import UIKitclass StudentsInfo{var StudentsCollection=[Student]()init(){let nameOfStudents=["zhangyi","zhanger","zhangsan","zhangsi","zhangwu"]let scoreOfStudents=[99,98,97,99,98]for i in 0...(nameOfStudents.count-1){let theStudent=Student(name:nameOfStudents[i],score:scoreOfStudents[i])StudentsCollection.append(theStudent)}}func addStudent() -> Student {let theStudent = Student(name: "新同學",score:100)StudentsCollection.append(theStudent)return theStudent}func deleteStudent(_ theStudent:Student) {if let theIndex = StudentsCollection.index(of: theStudent){StudentsCollection.remove(at: theIndex)}}func transferPosition(sourceIndex: Int, destinationIndex: Int) {if sourceIndex != destinationIndex {let theStudent = StudentsCollection[sourceIndex]StudentsCollection.remove(at: sourceIndex)StudentsCollection.insert(theStudent, at: destinationIndex)}return}
}
總結
以上是生活随笔為你收集整理的【ios开发/Xcode】使用UITableView完成学生信息及成绩的显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【ios开发/Xcode】实现登录注册
- 下一篇: Error #15: Initializ