簡體   English   中英

為什么我的 UITableDelegate 方法沒有被調用?

[英]Why are my UITableDelegate methods not being called?

我正在嘗試在 Swift 中創建一個非常簡單的 iOS 應用程序,僅僅只是一個Hello, world ,我無法在屏幕上顯示任何內容。 它可以毫無錯誤地構建和運行,但是當我在模擬設備或物理設備上運行它時,所有顯示的都是純黑屏幕。

這是主要的 class, AppDelegate

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var flowController: AppFlowController? // see below
    
    func application(
        _ application: UIApplication
        ,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        flowController = AppFlowController(window: window!)
        flowController!.start()
        
        return true
    }
}

這是一個 class 我希望最終用於隔離流邏輯:

class AppFlowController {
    let window: UIWindow
    
    init(window: UIWindow) {
        self.window = window
        print("flow controller constructed")
    }
    
    func start() {
        let viewController = DerivedViewController() // see below
        window.rootViewController = viewController
        window.makeKeyAndVisible()
        print("window brought to top")
    }
}

這是UITableViewController派生的 class 我正在嘗試但未能用於管理UITableView

class DerivedViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        print("viewDidLoad")
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        print("number of sections")
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("number of rows")
        return 4
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "Cell")
        cell.textLabel?.text = "Text"
        cell.detailTextLabel?.text = "Subtext"
        print("cell for row \(indexPath.row) created")
        
        return cell
    }
}

這是控制台 output:

flow controller constructed
viewDidLoad
number of sections
window brought to top

顯然tableView()委托方法永遠不會被調用,但如果我明白為什么不調用,我會很生氣。 我已經閱讀了 Apple 開發人員文檔和UITableViewController上的多個教程,但還沒有看到我缺少什么。 UITableViewController使自己成為其UITableView的代表,我明白嗎?

我不知道為什么會這樣,因為我對 Xcode 很陌生,但我注意到我的應用程序和我研究的一個工作示例有所不同,這就是我在info.plist中的值Bundle OS Type code$(PRODUCT_BUNDLE_PACKAGE_TYPE)並且起作用的是APPL 當我將我的更改為APPL時,它按預期顯示。

我猜這個變量沒有被正確解析,或者以某種方式被錯誤地設置; 我當然不是故意改變它,不知道它的存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM