斯威夫特| 为什么我的UITableView为空(自定义单元格)

Dav*_*vid 0 xcode uitableview ios swift

我的tableview应用看起来像这样:

空表视图

我不知道为什么这label1不起作用。

这是我的操作方法:

我拖动了一个UITableViewController并将其设置为initialViewController。

主板

我将UITableViewCell样式设置为 Custom

UITableViewCell样式

然后我设置UITableViewController的 Custom Class

UITableViewController类

和UITableViewCell的

UITableViewCell类

然后,我添加了一些约束:

UITableViewCell标签的约束

这里是TableViewController.swift

import UIKit

class TableViewController: UITableViewController {

    let arr = ["abc", "def", "ghi", "jkl", "mno"]

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

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1 // This should more than one if you want to see the sections and rows in your tableview
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("item", forIndexPath: indexPath) as! TableViewCell
        cell.label1.text = arr[indexPath.row]
        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 TableViewCell.swift

import UIKit

class TableViewCell: UITableViewCell {
    @IBOutlet weak var label1: UILabel!
}
Run Code Online (Sandbox Code Playgroud)

有人知道如何解决吗?我真的需要使用自定义单元格!

PS我正在使用Xcode 7 beta 5,Swift 2.0

小智 5

尝试这个

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
Run Code Online (Sandbox Code Playgroud)