我try 以编程方式实现UITableView,而不使用xib或故事板.这是我的代码:

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let table: UITableViewController = MyTableViewController()
        let tableView: UITableView = UITableView()
        tableView.frame = CGRect(x: 10, y: 10, width: 100, height: 500)
        tableView.dataSource = table
        tableView.delegate = table

        self.view.addSubview(tableView)
    }
}

MyTableViewController.swift

import UIKit

class MyTableViewController: UITableViewController {

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        NSLog("sections")
        return 2
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        NSLog("rows")
        return 3
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        NSLog("get cell")
        let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
        cell.textLabel!.text = "foo"
        return cell
    }  
}

但当我运行应用程序时,我得到的只是一张空桌子.在日志(log)中,我看到了几行sectionsrows,但没有get cell.如何修复此代码以获得包含6行foo文本的表格?

推荐答案

Note:正如你提到的,你在Swift年才开始编程.我通过编程创建了一个tableView.下面的Copypaste编码到你的viewControllerrun项目中...

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    private let myArray: NSArray = ["First","Second","Third"]
    private var myTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
        let displayWidth: CGFloat = self.view.frame.width
        let displayHeight: CGFloat = self.view.frame.height

        myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        myTableView.dataSource = self
        myTableView.delegate = self
        self.view.addSubview(myTableView)
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Num: \(indexPath.row)")
        print("Value: \(myArray[indexPath.row])")
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
        cell.textLabel!.text = "\(myArray[indexPath.row])"
        return cell
    }
}

Output:

enter image description here

Swift相关问答推荐

数据不会被ARC删除在swift'

SwiftUI—如何识别单词并获取视觉中的位置

SwiftUI:使视图B与视图A具有相同宽度的视图A更改比率?

插入/删除视图时的Swiftui动态过渡

如果通过计时器循环运行,则检索CPU利用率百分比的SWIFT脚本运行良好.似乎在没有计时器的情况下停留在初始百分比上

SWIFT`String.Encoding`:`.unicode`vs`.utf16`

在不传递参数的情况下使用Init方法?

如何观察UIViewRepresentable中的多个变化?

有条件地在同一视图上设置两个不同的过渡

使 tabview 垂直将视图移动到右侧 swift ui

如何绑定环境变量ios17

如何修改下面的代码,使图像 Select 的顺序与使用快速并发的图像呈现的顺序相匹配?

如何制作进度加载动画?

Vapor 4 身份验证问题 [用户未通过身份验证]

如何使 SwiftUI Picker 换行文本

为什么 switch 在 optional 中不接受 case nil?

在 iOS 中使用 Swift 保存 PDF 文件并显示它们

如何在 GCD 中停止 DispatchWorkItem?

Swift 2.0 按属性对对象数组进行排序

Objective-C 方法与可选需求方法 Swift 冲突