ios - TableView 未填充数据

标签 ios uitableview swift

所以我正在尝试实现一个简单的 UITableView,当我编译并运行我的代码时,它会显示一个空表。这是我的代码:

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
       var tableView = UITableView()
       var items:[String] = ["foo", "ban", "bar"]

    override func viewDidLoad() {
       super.viewDidLoad()
       tableView.frame = self.view.bounds
       self.view!.addSubview(tableView)
    }

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
       return self.items.count
    } 

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->    UITableViewCell! {
       var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as  UITableViewCell
       var theItem:String = items[indexPath.row]
       cell.textLabel.text = theItem
       return cell
    }
}

为什么表中没有显示项目?

最佳答案

好吧,你没有设置 tableViews 委托(delegate)和数据源,尝试这样做并确保你的 tableview 委托(delegate)/数据源方法被调用......

tableView.delegate = self
tableView.dataSource = self

关于ios - TableView 未填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25085917/

相关文章:

ios - 为什么我的 HTML 字符串在 WebView iOS 中没有显示正确的 html 标签?

ios - 更改 UITableView 的 textLabel 和 detailTextLabel 的默认颜色

swift - UITableView - 根据 UITableView 的行号在主视图 Controller 中执行不同的功能

ios - 需要在 iMessage Extension 的 insertText 方法上调用 didStartSending

iOS Swift 如何使用 <script> 标签解析 HTML 文件并获取其中的值

ios - 如何在 React-Native 应用程序中检索 iOS 状态栏高度?

ios - 如何在 Kony studio 应用程序中获取 iOS 设备 token

objective-c - 我想实现一半 UITableView 一半 MapView UIView,中间有一个按钮

ios - 使用 Scrollview 的 contentOffset 计算 Imageview Frame

ios - youtube api 上的 EXC_BAD_INSTRUCTION