我正在尝试让tableView在其单元格中显示内容,但是没有,我也不知道为什么。
这是代码。我还通过dataSource将表视图连接到了视图控制器,并在情节提要中委托了它,因此这就是为什么在viewDidLoad()方法中没有键入的原因。
class ViewController: UIViewController, MKMapViewDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
wordsArray = [Words(sectionName: "Example section", usedWords: ["aaaaa", "aaaaa", "aaaaa"])]
tableView.alpha = 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favCapitals.count
}
struct Words {
var sectionName: String! = ""
var usedWords: [String]! = []
}
var wordsArray: [Words] = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = wordsArray[indexPath.section].usedWords[indexPath.row]
cell.textLabel?.textColor = .black
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let sectionName = "Favourite Capitals"
return sectionName
}
答案 0 :(得分:0)
在监听cellForRowAt
之前,您需要调用以下方法:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
另一个常见的问题是未设置表格的框架(例如,在具有自动尺寸的UITableViewCell内)。该表格在获取单元格之前需要一个框架。
答案 1 :(得分:0)
您可以在viewDidLoad方法中使用tableView.reloadData()。