【问题标题】:tableView.reloadData() only called oncetableView.reloadData() 只调用一次
【发布时间】:2019-03-09 13:52:08
【问题描述】:

我对 Swift 还很陌生,我正在尝试让 tableView 出现在弹出窗口中,如此处所示。

我将数据源和委托设置为 self,并在从 Cloud Firestore 获取数据后调用 reloadData()。问题是,numberOfRowsInSection 可能会被调用一次,但不能再次被调用。 CellForRowAt 永远不会被调用。

这是否与我以编程方式制作 tableView 的事实有关?像它这样的东西并不认为框架是固定的或什么的,即使它是。如果我只是手动在 Xcode 中制作表格并链接插座,该表格确实有效。可悲的是我在不同的视图控制器中做同样的事情,但在那个视图控制器中它确实有效,我在代码中找不到任何差异。

这是按下按钮时调用的函数

@IBAction func ShowTeams(_ sender: Any) {
    RefreshData()
    let startPoint = CGPoint(x: self.btnShowTeams.frame.origin.x + 15, y: self.btnShowTeams.frame.origin.y + 23)
    tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
    tblTeams.register(UITableViewCell.self, forCellReuseIdentifier: "cellTeams")
    let popover = Popover()
    tblTeams.rowHeight = 35
    tblTeams.contentInset = UIEdgeInsets(top: 15,left: 0,bottom: 0,right: 0)
    tblTeams.separatorColor = UIColor(hexFromString: "13293d")
    popover.show(tblTeams, point: startPoint)
}

这里是设置tableView的函数

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if tableView == tblTeams{
        print("this shows like once, and yes there's data in dataTeams")
        return dataTeams.count
    }else{
        return 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == tblTeams{
        print("this doesnt show")
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellTeams", for: indexPath)
        cell.textLabel?.textAlignment = .center
        cell.textLabel?.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.bold)
        cell.accessoryType = .disclosureIndicator
        cell.textLabel!.text = dataTeams[indexPath.row]
        return cell
    }else{
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellInvites", for: indexPath)
        return cell
    }
}

这里是获取数据函数

func RefreshData(){
    let db = Firestore.firestore()
    let uid = Auth.auth().currentUser!.uid
    dataTeams = [String]()
    var i = 1
    while i <= 6 {
        db.collection("teams").whereField("uid\(i)", isEqualTo: uid)
            .getDocuments() { (querySnapshot, err) in
                if let err = err {
                    print("Error getting documents: \(err)")
                } else {
                    for document in querySnapshot!.documents {
                        self.dataTeams.append((document["username"] as? String)!)
                    }
                    print("the code does always make it here, I checked")
                    self.tblTeams.reloadData()
                }
        }
        i = i+1
    }
}

还有顶部的东西。谢谢!

import UIKit
import Firebase
import FirebaseFirestore
import GradientLoadingBar
import SCLAlertView
import Popover

class Game: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var btnShowTeams: UIButton!
var dataTeams = [String]()
var tblTeams: UITableView = UITableView()

override func viewDidLoad() {
    super.viewDidLoad()
    tblTeams.dataSource = self
    tblTeams.delegate = self
    RefreshData()
}

【问题讨论】:

  • 当您再次调用reloadData 时,只需打印tblTeams.dataSourcetblTeams.delegate 并检查您是否收到任何信息。
  • in ShowTeams 分配委托和数据源tblTeams.dataSource = self tblTeams.delegate = self并检查
  • 首先,如果它是一个弹出窗口,你应该使用 UIPopoverPresentationController 并从那里分离那个 tableview 代码,帮助你减少这样的错误。同样正如@Dharmesh 所说,检查是否设置了数据源和委托。
  • 当您检查两个对象引用是否指向同一个对象时,您应该使用=== 而不是==,所以我认为每个if tableView == tblTeams 都应该使用===

标签: ios swift uitableview


【解决方案1】:
@IBAction func ShowTeams(_ sender: Any) {
    RefreshData()
    let startPoint = CGPoint(x: self.btnShowTeams.frame.origin.x + 15, y: self.btnShowTeams.frame.origin.y + 23)
    tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))
    tblTeams.register(UITableViewCell.self, forCellReuseIdentifier: "cellTeams")
    let popover = Popover()
    tblTeams.rowHeight = 35
    tblTeams.contentInset = UIEdgeInsets(top: 15,left: 0,bottom: 0,right: 0)
    tblTeams.separatorColor = UIColor(hexFromString: "13293d")
    popover.show(tblTeams, point: startPoint)
}

在上面的代码中,您每次单击按钮时都会创建新的表格视图。并且新创建的 tableview 具有nil 数据源和委托(默认情况下)。

所以在上述方法中设置数据源或委托

tblTeams.dataSource = self
tblTeams.delegate = self

或通过替换来重用现有的表格视图

tblTeams = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180))

tblTeams.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 180)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-08
    • 2016-08-18
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多