gpt4 book ai didi

ios - Swift:TableView(单元格内)未加载 cellForRowAt 内的单元格或代码

转载 作者:行者123 更新时间:2023-11-30 11:04:49 24 4
gpt4 key购买 nike

为什么我的单元格不从单元格内的表格 View 加载数据?

基本上,它运行numberOfRowsInSection函数。但是,cellForRowAt 函数被完全忽略并且永远不会运行。

我可以向您保证我的所有单元格都正确链接,单元格内的表格 View 被委托(delegate),根表格 View 也是如此(我知道这一点,因为我在根表格 View 单元格中有其他数据可以正确加载)。

有人知道为什么这个函数内的代码根本不运行吗?

import Foundation
import UIKit

class SettingsTable: UITableViewCell, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var tableIcon: UIImageView!
@IBOutlet weak var tableTitle: UILabel!

var TableViewSettingsStruct = SettingsStruct()

func displayContent(tableViewTitle: String, tableViewIcon: UIImage, settingsStruct: SettingsStruct){
tableTitle.text = tableViewTitle
tableIcon.image = tableViewIcon
TableViewSettingsStruct = settingsStruct
}

override func awakeFromNib() {
super.awakeFromNib()
//Initialization
myTableView.delegate = self
myTableView.dataSource = self
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
myTableView.delegate = self
myTableView.dataSource = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableViewSettingsStruct.cellType.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("Running tableView Code")
let cell = tableView.dequeueReusableCell(withIdentifier: "errorCell") as! UITableViewCell
cell.textLabel?.text = "An error occurred fetching the cell"
return cell
}
}

如果您注意到的话,我在 cellForRowAt 函数中添加了一个打印内容。与我实现的其他 tableView 不同,此打印语句不会显示在控制台上(即它不会运行)。

最佳答案

好的,谢谢大家的帮助!

我发现我的 tableView 没有设置高度约束,一旦设置了高度约束,tableView 就会加载单元格(因此会加载 cellForRowAt 函数)。

有时,这可能会很乏味:/

关于ios - Swift:TableView(单元格内)未加载 cellForRowAt 内的单元格或代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843384/

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