簡體   English   中英

發出使用Swift從firebase獲取自定義單元類中的數據的問題

[英]Issue fetching data in custom cell class from firebase using Swift

我已經創建了一個自定義單元類,它從firebase獲取數據。 一切都運行正常但發生的事情是添加新數據時,顯示在底部而不是頂部。 在我提到的代碼中插入新項目到索引路徑0.仍然代碼不起作用

這是代碼..

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

    @IBOutlet weak var save: UIButton!
    @IBOutlet weak var table: UITableView!

    var firebase = Firebase(url: "https://meanwhile.firebaseio.com")
    var messages = [String]()
    var uid:String = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        var localArray = [String]()
        firebase.observeEventType(.ChildAdded, withBlock: { snapshot in

            //print(snapshot.value)

            let msgText = snapshot.value.objectForKey("text") as! String
            localArray.insert(msgText, atIndex: 0)
            self.messages = localArray
            self.table.reloadData()
        }
     }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.table.dequeueReusableCellWithIdentifier("Vish", forIndexPath: indexPath) as! CustomTableViewCell

        let fdata = self.messages[indexPath.item]
       cell.data.text = fdata as? String

        return cell
     }
}

我沒有在這里看到代碼,但是你提到你正在使用自定義表格單元來加載數據。

如果要在CustomTableViewCell類中加載數據,則必須非常小心如何執行此操作,或者將其中的單元格的數據加載移動到其他位置。

這是因為dequeueReusableCellWithIdentifier UITableView不會為表的任何行創建UITableViewCell ,而是維護一個較小的單元隊列,它重用它來減少內存使用並提高性能。

返回的UITableViewCell對象通常是應用程序因性能原因而重用的對象。

檢查您是否正在共享狀態(您不希望如此)和/或在CustomTableViewCell內部具有競爭條件。

這些錯誤通常表現為表格視圖數據出現在錯誤的單元格中。

來源: https//developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView : cellForRowAtIndexPath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM