我已经在InterfaceBuilder中设置了以下屏幕:

ios - UITableView不显示UITableViewCells-LMLPHP

连接的IBOutlets和所需的功能,以符合UITableViewDelegateUITableViewDataSource协议。尚无太多功能-只是结构开始。
这是ViewController的代码:

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var AllCoopsLabel: UILabel!
    @IBOutlet weak var RecentCoopsLabel: UILabel!
    @IBOutlet weak var LatestNotificationsLabel: UILabel!
    @IBOutlet weak var OpenCoops: UILabel!
    @IBOutlet weak var CompletedCoops: UILabel!
    @IBOutlet weak var Earnings: UILabel!
    @IBOutlet weak var OpenCoopsLabel: UILabel!
    @IBOutlet weak var CompletedCoopsLabel: UILabel!
    @IBOutlet weak var EarningsLabel: UILabel!
    @IBOutlet weak var ShowAllCoopsButton: UIButton!
    @IBOutlet weak var ShowAllNotificationsButton: UILabel!
    @IBOutlet weak var RecentCoopsTable: UITableView!
    @IBOutlet weak var LatestNotificationsTable: UITableView!
    let apiService = APIService()
    var parentController: TabBarController?
    var user: User?



    override func viewDidLoad() {
        super.viewDidLoad()
        RecentCoopsTable.dataSource = self
        RecentCoopsTable.delegate = self
        LatestNotificationsTable.dataSource = self
        LatestNotificationsTable.delegate = self

        let token = UserDefaults.standard.string(forKey: "authtoken")!
        self.apiService.getUserFromAuthtoken(token: token, completion: {result in
            switch result {
            case .success(let user):
                DispatchQueue.main.async {
                    self.user = user
                    self.parentController = self.tabBarController as? TabBarController
                    self.parentController?.user = user
                    Globals.shared.user = user
                }
            case .failure(let error):
                print("An error occured \(error.localizedDescription)")
            }
        })
    }



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



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("Was here")
        let coopCell = tableView.dequeueReusableCell(withIdentifier: "RecentCoopsViewCell", for: indexPath) as! CoopViewCell
        coopCell.CoopName.text = "foo bar"
        return coopCell
    }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }



    @IBAction func showAllCooperationsButtonPressed(_ sender: Any) {
        print("showAllCooperationsButtonPressed")
    }



    @IBAction func showAllNotificationsButtonPressed(_ sender: Any) {
        print("showAllNotificationsButtonPressed")
    }
}



class CoopViewCell: UITableViewCell {


    @IBOutlet weak var CoopName: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}



class NotificationViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

从我的角度来看,我应该看到3个表行显示“foo bar”。
但是我没有。就拿这个。

ios - UITableView不显示UITableViewCells-LMLPHP

也没有得到3个预期的“在这里”控制台日志。
我错过了什么?

最佳答案

根据Apple Developer site

尝试在API调用后重新加载tableView。

tableView.reloadData()

关于ios - UITableView不显示UITableViewCells,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60569380/

10-14 19:58
查看更多