【问题标题】:Multiple Custom Cells in UITableView With Firebase Database带有 Firebase 数据库的 UITableView 中的多个自定义单元格
【发布时间】:2018-07-10 21:15:59
【问题描述】:

我想创建一个包含两个自定义单元格的表格视图,其中的信息是从 Firebase 数据库中提取的。第一个自定义单元格显示日期,第二个自定义单元格显示事件。当我运行应用程序时,tableview 只返回第一个自定义单元格,即日期。这是什么原因造成的?

import UIKit
import Firebase

class augustController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var augustController: UITableView!

    var ref = DatabaseReference()

    var date = [String]()
    var event = [String]()

    var databaseHandle:DatabaseHandle = 0
    var databaseHandle2:DatabaseHandle = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        ref = Database.database().reference()

        databaseHandle = ref.child("Events").child("August").child("dates").observe(.childAdded) { (snapshot) in
            let post = snapshot.value as? String
            if let actualPost = post {
                self.date.append(actualPost)
            }
        }

        databaseHandle2 = ref.child("Events").child("August").child("events").observe(.childAdded) { (snapshot) in
            let post2 = snapshot.value as? String
            if let actualPost2 = post2 {
                self.event.append(actualPost2)
                self.augustController.reloadData()
            }
        }
    }

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {            
        let augustDate = tableView.dequeueReusableCell(withIdentifier: "augustDate") as! dateCell
        augustDate.date.text = date[indexPath.row]

        return(augustDate)
    }

    func tableView2(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let augustEvents = tableView.dequeueReusableCell(withIdentifier: "augustEvents") as! eventCell
        augustEvents.even.text = event[indexPath.row]

        return(augustEvents)
    }
}

【问题讨论】:

  • 既然您已经表明该问题已得到解答,请返回您之前的问题并在适当的情况下执行相同操作。

标签: ios swift uitableview firebase-realtime-database


【解决方案1】:

你执行cellForRow两次,你必须

var itemsArr = [Item]()

//

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return itemsArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  let item = itemsArr[indexPath.row]

   if item.isDate {

     let augustDate = tableView.dequeueReusableCell(withIdentifier: "augustDate") as! dateCell
     augustDate.date.text = item.content
     return augustDate

 }
 else {

    let augustEvents = tableView.dequeueReusableCell(withIdentifier: "augustEvents") as! eventCell
    augustEvents.even.text = item.content
    return augustEvents
 }
}

//

然后将其设为一个数组并附加此结构类型的项目

struct Item {
   var isDate:Bool
   var content:String
}

【讨论】:

  • 还有两个numberOfRowsInSection 方法。除了组合两个cellForRowAt 方法之外,还需要解决更多问题。
  • viewDidLoad 中的@Sh_Khan 仍然是self.event.append(actualPost2)self.date.append(actualPost),还是两者都是self. itemsArr.append(actualPost2)self. itemsArr.append(actualPost)
猜你喜欢
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
相关资源
最近更新 更多