美文网首页
swift 反向传值

swift 反向传值

作者: 你又脸红了i | 来源:发表于2019-01-18 14:32 被阅读0次
``` import UIKit

class TableVC: UITableViewController {
    
    var sectionName:[String] = []
    var rowName1:[String] = []
    var rowName2:[String] = []
    var rowName3:[String] = []
    var rowName4:[String] = []
    
    var cellNum:Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "周考成绩单"
        
        sectionName = ["1","2","3","7"]
    
        self.tableView.delegate = self
        self.tableView.dataSource = self
        
        self.tableView.tableFooterView = UIView()
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "全部", style: .plain, target: self, action: #selector(whole))
    }
    
    @objc func whole() {
        
        if rowName1.count>0{
            print("刘依男:\(rowName1[0]),\(rowName1[1]),\(rowName1[2])")
        }
        if rowName2.count>0{
            print("刘浩:\(rowName2[0]),\(rowName2[1]),\(rowName2[2])")
        }
        if rowName3.count>0{
           print("徐才茵:\(rowName3[0]),\(rowName3[1]),\(rowName3[2])")
        }
        if rowName4.count>0{
            print("隆佳佳:\(rowName4[0]),\(rowName4[1]),\(rowName4[2])")
        }
        
       
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        #warning ("Incomplete implementation, return the number of sections")
        return sectionName.count
        return 5
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        #warning ("Incomplete implementation, return the number of rows")
    switch section {
        case 0:
            return rowName1.count
        case 1:
            return rowName2.count
        case 2:
            return rowName3.count
        case 3:
            return rowName4.count
        default:
            return 0
    }
        return 12
}

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell==nil{
            
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        }
        // Configure the cell...
        
        switch indexPath.section {
        case 0:
            cell.textLabel?.text = rowName1[indexPath.row]
        case 1:
            cell.textLabel?.text = rowName2[indexPath.row]
        case 2:
            cell.textLabel?.text = rowName3[indexPath.row]
        case 3:
            cell.textLabel?.text = rowName4[indexPath.row]
        default:
            ""
        }
        
        return cell!
    }
    
//    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//
//        return sectionName[section]
//    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 80
        
    }
    
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
        view.backgroundColor = UIColor.lightGray
        
        let btn = UIButton(frame: CGRect(x:UIScreen.main.bounds.width-80-10, y: 10, width: 80, height: 30))
        btn.setTitle("录入分数", for: .normal)
        btn.addTarget(self, action: #selector(buton), for: .touchUpInside)
        btn.setTitleColor(UIColor.red, for: .normal)
        view.addSubview(btn)
        btn.tag = 100+section
        
        let label = UILabel(frame: CGRect(x: 10, y: 10, width: 60, height: 30))
        view.addSubview(label)
        label.text = sectionName[section]
        
        let lineView = UIView(frame: CGRect(x: 0, y: 49, width: UIScreen.main.bounds.width, height: 1))
        view.addSubview(lineView)
        lineView.backgroundColor = UIColor.black
        return view
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 50
    }
    
    @objc func buton(but:UIButton){
        
        print(but.tag)
        
        let vc = ScoreWriteVC()
        vc.jumpScore = {
            
            (str1,str2,str3)->() in
            
            switch but.tag {
            case 100:
                self.rowName1.removeAll()
                self.rowName1.append("语文:\(str1)")
                self.rowName1.append("数学:\(str2)")
                self.rowName1.append("英语:\(str3)")
            case 101:
                self.rowName2.removeAll()
                self.rowName2.append("语文:\(str1)")
                self.rowName2.append("数学:\(str2)")
                self.rowName2.append("英语:\(str3)")
            case 102:
                self.rowName3.removeAll()
                self.rowName3.append("语文:\(str1)")
                self.rowName3.append("数学:\(str2)")
                self.rowName3.append("英语:\(str3)")
            case 103:
                self.rowName3.removeAll()
                self.rowName3.append("语文:\(str1)")
                self.rowName3.append("数学:\(str2)")
                self.rowName3.append("英语:\(str3)")
            default:
                ""
            }
            self.tableView.reloadData()
        }
        self.navigationController?.pushViewController(vc, animated: true)
    }

   

}

新建

import UIKit

class ScoreWriteVC: UIViewController {

    typealias blockScore = (String,String,String)->()
    var jumpScore:blockScore?
    
    var tf1:UITextField!
    var tf2:UITextField!
    var tf3:UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.navigationItem.title = "分数录入"
        
        view.backgroundColor = UIColor.white
        
        let lab1 = UILabel(frame: CGRect(x: 30, y: 200, width:60 , height: 60))
        view.addSubview(lab1)
        lab1.text = "语文"
        
        let lab2 = UILabel(frame: CGRect(x: 30, y: 200+lab1.frame.height+10, width:60 , height: 60))
        view.addSubview(lab2)
        lab2.text = "数学"
        
        let lab3 = UILabel(frame: CGRect(x: 30, y: 200+lab1.frame.height+10+lab2.frame.height+10, width:60 , height: 60))
        view.addSubview(lab3)
        lab3.text = "英语"
        
        tf1 = UITextField(frame: CGRect(x: 50+lab1.frame.width+5, y: 200, width: UIScreen.main.bounds.width-(50+lab1.frame.width+5)-10, height: 60))
        view.addSubview(tf1)
        tf1.placeholder = "请输入分数"
        tf1.borderStyle = .roundedRect
        
        tf2 = UITextField(frame: CGRect(x: 50+lab1.frame.width+5, y: 200+tf1.frame.height+10, width: UIScreen.main.bounds.width-(50+lab1.frame.width+5)-10, height: 60))
        view.addSubview(tf2)
        tf2.placeholder = "请输入分数"
        tf2.borderStyle = .roundedRect
        
        tf3 = UITextField(frame: CGRect(x: 50+lab1.frame.width+5, y: 200+tf1.frame.height+10+tf2.frame.height+10, width: UIScreen.main.bounds.width-(50+lab1.frame.width+5)-10, height: 60))
        view.addSubview(tf3)
        tf3.placeholder = "请输入分数"
        tf3.borderStyle = .roundedRect
        
        let btn1 = UIButton(frame: CGRect(x: (UIScreen.main.bounds.width-100)/2-60, y: 200+lab1.frame.height+10+lab2.frame.height+10+lab3.frame.height+30, width: 100, height: 80))
        view.addSubview(btn1)
        btn1.setTitle("保存", for: .normal)
        btn1.addTarget(self, action: #selector(save), for: .touchUpInside)
        btn1.setTitleColor(UIColor.red, for: .normal)
        
        let btn2 = UIButton(frame: CGRect(x: (UIScreen.main.bounds.width-100)/2+60, y: 200+lab1.frame.height+10+lab2.frame.height+10+lab3.frame.height+30, width: 100, height: 80))
        view.addSubview(btn2)
        btn2.setTitle("返回", for: .normal)
        btn2.addTarget(self, action: #selector(back), for: .touchUpInside)
        btn2.setTitleColor(UIColor.red, for: .normal)
    }
    
    @objc func save() {
        
        self.jumpScore!(tf1.text!,tf2.text!,tf3.text!)
        
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func back() {
        
        self.navigationController?.popViewController(animated: true)
    }
    
}

相关文章

  • swift 反向传值

    新建

  • swift 代理传值

    swift中的代理传值跟oc中的用法基本一样,都是用于反向传值,这里假设a界面向b界面传值为正向传值,则: 在b界...

  • Swift正反向传值

    正向传值: 1、在B界面中声明一个公开接收的属性,可以是字符串或者整形变量 var passValue = "" ...

  • 【iOS开发细节】之- delegate代理的使用

    在iOS开发中、好多时候需要涉及到页面传值、而传值又分为正向传值和反向传值 一、 传值 1、正向传值 2、反向传值...

  • OC中反向传值的方法

    oc中反向传值四种方法 block反向传值 在需要传值的界面: 在接受到传值的界面 单例反向传值 创建一个单例类 ...

  • reactNative 之组件传值和反向传值

    在项目中我们经常会遇到传值,传值有正向传值和反向传值,比如1.正向传值:从A组件push到B组件传值2.反向传值:...

  • swift 闭包传值

    在oc 中,反向传值可以采用block块来实现,同样,在swift 中也有类似的闭包,下面就闭包传值进行简单的介绍...

  • React-Natvie Navigtor正向反向传值

    正向传值 利用属性反向传值

  • 传值

    在之前也写过正反向传值的文章,不过语言是Swift的(http://www.jianshu.com/p/be608...

  • swift 闭包反向传值

    1.首先在要传值的页面 定义typealias TestBlock = (model: 要传数据的类型)->()2...

网友评论

      本文标题:swift 反向传值

      本文链接:https://www.haomeiwen.com/subject/lmjndqtx.html