【问题标题】:tableView not showing data valuestableView 不显示数据值
【发布时间】:2019-03-22 02:01:58
【问题描述】:

我试图让我的 tableView 在其单元格中显示内容,但它没有,我不知道为什么。

这里是代码。我还通过 dataSource 将 table view 连接到 viewcontroller 并在 storyboard 中委托,所以这就是为什么在 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
}

【问题讨论】:

    标签: swift xcode uitableview


    【解决方案1】:

    在它侦听cellForRowAt之前,您需要调用以下方法:

    override func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
    

    另一个常见问题是表格的框架未设置(例如在 UITableViewCell 内,具有自动尺寸)。表格在获取单元格之前需要一个框架。

    【讨论】:

      【解决方案2】:

      可以在 viewDidLoad 方法中使用 tableView.reloadData()。

      【讨论】:

        猜你喜欢
        • 2012-01-04
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-18
        相关资源
        最近更新 更多