Swift你 Tableview application
程序员文章站
2022-06-08 13:55:07
...
application
let vc = ViewController()
let nav = UINavigationController(rootViewController: vc)
self.window?.rootViewController=nav
默认控制器
import UIKit
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
var table = UITableView()
var Arr = NSArray()
override func viewWillAppear(_ animated: Bool) {
table.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title="周考成绩单"
table=UITableView(frame: UIScreen.main.bounds, style: .grouped)
table.delegate=self
table.dataSource=self
self.view.addSubview(table)
Arr = ["A","B","C","D","E","F"]
let right = UIBarButtonItem(barButtonSystemItem: .add, target: self, action:#selector(abc))
self.navigationItem.rightBarButtonItem=right
}
func numberOfSections(in tableView: UITableView) -> Int {
return 6
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
var headers = self.Arr
return headers[section] as! String
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
}
let Bun = UIButton(frame: CGRect(x: 300, y: 10, width: 100, height: 30))
Bun.setTitle("录入分数", for: .normal)
Bun.setTitleColor(.red, for: .normal)
Bun.addTarget(self, action: #selector(TZhuan), for: .touchUpInside)
cell.addSubview(Bun)
let sd:AppDelegate = UIApplication.shared.delegate as! AppDelegate
print(sd.str)
if indexPath.section==0 {
cell.textLabel?.text = sd.str
}
return cell
}
@objc func TZhuan(){
let sco = ScoreViewController()
self.navigationController?.pushViewController(sco, animated: true)
}
@objc func abc(){
print("点击了添加按钮")
}
}
跳转新页面
import UIKit
class ScoreViewController: UIViewController {
var Language = UITextField()
var Math = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor=UIColor.white
Language = UITextField(frame: CGRect(x: 100, y: 200, width: 240, height: 40))
Language.backgroundColor=UIColor.lightGray
Language.placeholder="语文"
Math = UITextField(frame: CGRect(x: 100, y: 300, width: 240, height: 40))
Math.backgroundColor=UIColor.lightGray
Math.placeholder="数学"
let English = UITextField(frame: CGRect(x: 100, y: 400, width: 240, height: 40))
English.backgroundColor=UIColor.lightGray
English.placeholder="英语"
self.view.addSubview(Language)
self.view.addSubview(Math)
self.view.addSubview(English)
let Save = UIButton(frame: CGRect(x: 80, y: 600, width: 100, height: 50))
Save.setTitle("保存", for: .normal)
Save.setTitleColor(.red, for: .normal)
Save.addTarget(self, action: #selector(SaveBun), for:.touchUpInside)
let Back = UIButton(frame: CGRect(x: 250, y: 600, width: 100, height: 50))
Back.setTitle("返回", for: .normal)
Back.setTitleColor(.red, for: .normal)
self.view.addSubview(Save)
self.view.addSubview(Back)
Back.addTarget(self, action:#selector(BackBun), for: .touchUpInside)
}
@objc func BackBun(){
// let vc = ViewController()
self.navigationController?.popViewController(animated: true)
}
@objc func SaveBun(){
// let vc = ViewController()
let sd:AppDelegate = UIApplication.shared.delegate as! AppDelegate
sd.str = Language.text!
self.navigationController?.popViewController(animated: true)
}
}