TableView Swift具有动态和固定单元格

时间:2017-02-16 22:24:36

标签: swift xcode firebase firebase-realtime-database tableview

我制作的应用程序显示来自Firebase的数据,我有一台电视必须显示一个固定的单元格(选择要显示的数据类型),另一个单元格显示来自服务器的数据这是代码:

import UIKit
import Firebase
import SwiftDate

class EventiTVC: UITableViewController {

    let userID = FIRAuth.auth()?.currentUser?.uid
    let ref = FIRDatabase.database().reference()        
    let utente = UserDefaults.standard

    @IBOutlet weak var Menu_button: UIBarButtonItem!

    var Eventi:[String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        if self.revealViewController() != nil {
            Menu_button.target = self.revealViewController()         
            Menu_button.action = #selector(SWRevealViewController.revealToggle(_:))                
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }

        let imageView = UIImageView(image: #imageLiteral(resourceName: "Hangover_Background"))
        imageView.contentMode = .scaleAspectFill

        self.tableView.backgroundView = imageView
        self.tableView.tableFooterView = UIView()            
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white,NSFontAttributeName: UIFont(name: "HelveticaNeue-Bold", size: 19)!]

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if self.revealViewController() != nil {
            Menu_button.target = self.revealViewController()                
            Menu_button.action = #selector(SWRevealViewController.revealToggle(_:))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }

        self.navigationController?.navigationBar.barTintColor = UIColor.orange
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "HelveticaNeue-Bold", size: 19)!,NSForegroundColorAttributeName : UIColor.white]

        DeterminaInfoProprietario()
        DeterminoLocali()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows

        if section == 0{
            return 1
        }

        else {
            return Eventi.count
        }

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.row == 0{
            let cell = Bundle.main.loadNibNamed("Event_Type", owner: self, options: nil)?.first as! Event_Type                
            return cell
        }

        else {
            let cell = Bundle.main.loadNibNamed("Evento", owner: self, options: nil)?.first as! Evento
            let EventoCorrente = Eventi[indexPath.row]
            // scarico info Evento corrente                 ref.child("Eventi").child(EventoCorrente).observeSingleEvent(of: .value, with: { (snap) in

                if snap.childrenCount != 0 {
                    let DatiEvento = snap.value as? NSDictionary
                    cell.Nome.text = DatiEvento?.value(forKey: "Nome") as! String!
                    cell.Locale.text = DatiEvento?.value(forKey: "Locale") as! String!

                    let urlcopertina = DatiEvento?.value(forKey: "Immagine Copertina") as! String!
                    cell.Copertina.aail_load(url: NSURL(string: urlcopertina!)!)

                    // per scaricare il colore , devo scaricare il locale
                    let locale = DatiEvento?.value(forKey: "Locale") as! String!                        
                    var Colore:UIColor? = nil             
                    var Ombra:UIColor? = nil

                    self.ref.child("Locali").child(locale!).observeSingleEvent(of: .value, with: { (snap2) in

                        if snap2.childrenCount != 0 {
                            let DatiLocale = snap2.value as? NSDictionary                                
                            Colore = ColoriDaStringa(Colore: (DatiLocale?.value(forKey: "Colore Pagina"))! as! String)[0]
                            Ombra = ColoriDaStringa(Colore: (DatiLocale?.value(forKey: "Colore Pagina"))! as! String)[1]
                            cell.ViewC.backgroundColor = Colore!
                            cell.View_ombra.backgroundColor = Ombra!
                        }
                    })
                }

                else {
                    // Evento Cancellato? // gestire situazione
                }
            })
            return cell
        }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{

        if indexPath.row == 0{
            return 44                
        }else {
            return 190
        }
    }

    @IBAction func ritornaHome (Segue:UIStoryboardSegue){

    }
}

This is what I got

我有2个静态单元和1个动态单元,它应该是相反的,任何想法为什么?

3 个答案:

答案 0 :(得分:0)

cellForRowAtIndexPath()方法中,您应该更改if statement

if (indexPath.row == 0)

if (indexPath.section == 0)

答案 1 :(得分:0)

将这两个功能修改为:

 override func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

   return Eventi.count+1

}

答案 2 :(得分:0)

我们应该使用最新功能。你应该使用自动布局。自动布局具有内置功能,可显示UITableView Cell的动态高度。如果您想学习,请参阅以下链接。

Dynamic cell height UITableview

否则,我们会在您的代码中回答问题。

numberOfSections()中你设置了错误的条件。只需替换

        return 2

return 1

numberOfRowsInSection()替换

if section == 0{
        return 1
    }

    else {
        return Eventi.count
    }

   return Eventi.count+1