NFCViewController.swift 2.85 KB
//
//  NFCViewController.swift
//  Twear
//
//  Created by yangbin on 2022/2/12.
//

import UIKit
import CoreNFC

@available(iOS 13.0, *)
class NFCViewController: UIViewController {
    
    var nfcList: [NFCModel] = []
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocString("卡包")
        
        let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 28))
        addButton.setImage(UIImage(named: "device_add"), for: .normal)
        addButton.addTarget(self, action: #selector(add), for: .touchUpInside)
        navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: addButton)
        
        tableView.register(UINib.init(nibName: "NFCCell", bundle: Bundle.main), forCellReuseIdentifier: "NFCCell")
        tableView.contentInset = UIEdgeInsets.init(top: 5, left: 0, bottom: 10, right: 0)
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        // Do any additional setup after loading the view.
    }
    
    @objc private func add() {
        let vc1 = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Setting", identifier: "NFCHomeVC")
        navigationController?.pushViewController(vc1, animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
@available(iOS 13.0, *)
extension NFCViewController: UITableViewDelegate, UITableViewDataSource {


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return SCREEN_WIDTH/7.0*3.0+10
    }
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nfcList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NFCCell", for: indexPath) as! NFCCell
        cell.nfc = nfcList[indexPath.row]
        return cell
    }
    

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        let cell = tableView.cellForRow(at: indexPath)
//        cell.isSelected = false
        let vc = UIStoryboard.loadViewControllerIdentifier(storyboardName: "Setting", identifier: "NFCDetailVC") as! NFCDetailVC
        vc.nfc = nfcList[indexPath.row]
        vc.index = indexPath.row
        vc.nfcClosure = { [weak self]  in
            self?.nfcList = UserInfo.nfc
//            self?.nfcList[indexPath.row] = nfc1
            self?.tableView.reloadData()
        }
        navigationController?.pushViewController(vc, animated: true)

    }
}