【ios开发/Xcode】使用UITableView完成学生信息及成绩的显示

【ios开发/Xcode】使用UITableView完成学生信息及成绩的显示

设计思想

首先创建所有页面的故事版,包括,登录、注册与成绩页面
在这里插入图片描述
在这里插入图片描述
接着设置故事版的关联代码,如下图所示为用户名与密码UITextField的关联
在这里插入图片描述
再进行一些空间的格式类型等设置,如下图所示为TableViewCell单元格属性的设置
在这里插入图片描述
最后在增加增删改查的代码,完成设计

实现效果

注册账号:lct,密码:lct,登录系统
在这里插入图片描述
进入系统,看到学生成绩信息,进行编辑操作/删除操作/增加操作
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

源代码

注:@开头的这些代码都是需要关联控键,都需要自行在故事板中(Storyboards)进行关联
登录界面

//登录界面
import  UIKit

class 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! String
        let password1 = userInfo["password"] as! String
        userId.text = userId1
        password.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 in
            
            let 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:String
    var score:Int
    
    init(name:String,score:Int){
        self.name=name
        self.score=score
        super.init()
    }
}

定义增加、删除、移动等操作

//定义增加、删除、移动等操作
import UIKit

class 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
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值