Tableview不显示数据

时间:2018-03-29 09:59:49

标签: ios iphone swift

SecondVC.swift
这是tableview和滑块视图的代码文件,但输出中缺少tableview数据。 请帮帮我 代码看起来很完美,但单元格仍未显示从数据库中提取的内容 单元格正在显示但没有显示内容 实际上,显示的单元格数不等于fields.count

O/p + Main.storyboard

File Hierarchy

import UIKit
import FirebaseAuth
import FirebaseDatabase

class SecondVC: UIViewController,UITableViewDelegate,UITableViewDataSource{

    @IBOutlet weak var tableview: UITableView!
    @IBOutlet weak var blurView: UIVisualEffectView!
    @IBOutlet weak var sideView: UIView!
    @IBOutlet weak var viewConstraint: NSLayoutConstraint!

    var field = [Fields]()

    func tableView(_ tableView: UITableView, numberOfRowsInSection section:
        Int) -> Int {
        return field.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath:
        IndexPath) -> UITableViewCell {
        let cell = tableview.dequeueReusableCell(withIdentifier:"SecondVCcell")
        cell?.selectionStyle = .none
        cell?.textLabel?.text = field[indexPath.row].Field_fieldname
        cell?.detailTextLabel?.text = field[indexPath.row].Field_fieldvalue
        return cell!
    }

    var name : String?
    override func viewDidLoad() {
        super.viewDidLoad()

        tableview.delegate = self
        tableview.dataSource = self
        blurView.layer.cornerRadius = 15
        sideView.layer.shadowColor = UIColor.black.cgColor
        sideView.layer.shadowOpacity = 0.8
        sideView.layer.shadowOffset = CGSize(width : 5, height : 0)

        viewConstraint.constant = -175
        loadData()
    }

    func loadData()
    {
        let userID = Auth.auth().currentUser!.uid


        if let dict = snapshot.value as? [String : Any]
        {
            let fieldV = dict["fieldN"] as! String
            let fields = Fields(fn: snapshot.key , fv: fieldV)
            self.field.append(fields)
            self.tableview.reloadData()
        }
    }

    @IBAction func PanGestureTriggered(_ sender: UIPanGestureRecognizer) {



        if sender.state == .began || sender.state == .changed
        {
            let translate = sender.translation(in: self.view).x
            if translate > 0
            {
                if viewConstraint.constant < 20
                {
                    UIView.animate(withDuration: 0.2, animations: {
                        self.viewConstraint.constant += translate/10
                        self.view.layoutIfNeeded()
                    })
                }
            }
            else
            {
                if viewConstraint.constant > -175
                {
                    UIView.animate(withDuration: 0.2, animations: {
                        self.viewConstraint.constant += translate / 10
                        self.view.layoutIfNeeded()
                    })
                }
            }
        }
        else if sender.state == .ended
        {
            if viewConstraint.constant < -100
            {
                UIView.animate(withDuration: 0.2, animations: {
                    self.viewConstraint.constant = -175
                    self.view.layoutIfNeeded()
                }

                )}
            else
            {
                UIView.animate(withDuration: 0.2, animations: {
                    self.viewConstraint.constant = 0
                    self.view.layoutIfNeeded()
                }
                )}


        }
    }
    @IBAction func SignOutTapped(_ sender: Any) {
        let firebaseAuth = Auth.auth()
        if firebaseAuth.currentUser != nil
        {
            do {
                try firebaseAuth.signOut()
            } catch let signOutError as NSError {
                print ("Error signing out: %@", signOutError)
            }
        }
        if firebaseAuth.currentUser == nil
        {
            let loginVC = UIStoryboard(name: "Main",
                                       bundle:nil).instantiateViewController(withIdentifier: "Login") as!
            ViewController
            self.present(loginVC, animated: true, completion: nil)
        }
    }
    @IBAction func HomeButtonTapped(_ sender: UIButton) {
        UIView.animate(withDuration: 0.2, animations: {
            self.viewConstraint.constant = -175
            self.view.layoutIfNeeded()
        }
        )}
    @IBAction func unwindSegue(_ sender: UIStoryboardSegue)
    {
    }
}

1 个答案:

答案 0 :(得分:0)

如果您使用自定义表格单元格,则表示尚未注册Nib。

tableView.register(UINib(nibName: "", bundle: nil), forCellWithReuseIdentifier:"")

如果您正在使用原型单元格,则必须使字段数组计数为0