繁体   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