gpt4 book ai didi

swift - UITableViewCell 不显示数据

转载 作者:行者123 更新时间:2023-11-28 11:58:58 25 4
gpt4 key购买 nike

我有一个 TableView 单元格,它应该只显示一个标签,但当我运行我的应用程序时它根本不会显示任何内容。我什至尝试更改我的代码中的 textLabels 但仍然没有。我也尝试更改在 Storyboard中起作用的单元格背景颜色,但是当我实际运行该应用程序时,没有任何变化。我已经完全断开我的 View Controller 以查看我的代码是否是问题所在,但仍然没有任何反应,所以从我看到的不是我的代码而是 View 本身。我可能是错的。这是我现在的观点:tableviewdata

//
// TableViewController.swift
// streamingVideo
//
// Created by Connor Woodford on 5/15/18.
// Copyright © 2018 Connor Woodford. All rights reserved.
//

import UIKit

struct Courses: Decodable {
let id: String
let title: String
let location: String

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {



var courses = [Courses]()

@IBOutlet weak var arg: UITableView!


override func viewDidLoad() {
super.viewDidLoad()

fetchJSON()


// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}

func fetchJSON() {

let urlString = "http://homevideostuff.192.168.1.14.xip.io:8888/getClasses.php"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, _, err) in
DispatchQueue.main.async {
if let err = err {
print("Failed to get data from URL:", err)
return
} else {

print("Loaded URL")
}

guard let data = data else { return }

do {
let decoder = JSONDecoder()
self.courses = try decoder.decode([Courses].self, from: data)
print(self.courses)

} catch let jsonErr {
print("Failed to decode JSON", jsonErr)
}
}

}.resume()
}

// MARK: - Table view data source

func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return courses.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

let sweet = courses[indexPath.row]

let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.black
cell.selectedBackgroundView = bgColorView

cell.textLabel?.text = "sdafasdfs"
cell.detailTextLabel?.text = sweet.location

print("azDFASDFASDFAS")
return cell
}
}

最佳答案

  func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}

您需要返回至少 1 个部分,因为一行是一个部分的子级

关于swift - UITableViewCell 不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50395005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com