在Swift中以编程方式创建一个UITableViewController
发布时间:2020-12-14 05:24:07 所属栏目:百科 来源:网络整理
导读:正如标题所说,我试图以编程方式设置UITableViewController.经过几个小时的尝试,我希望有人可以帮助我.而且,是的,我已经检查了有关此事的其他帖子: import UIKitclass MainViewController: UITableViewController { init(style: UITableViewStyle) { super.i
正如标题所说,我试图以编程方式设置UITableViewController.经过几个小时的尝试,我希望有人可以帮助我.而且,是的,我已经检查了有关此事的其他帖子:
import UIKit class MainViewController: UITableViewController { init(style: UITableViewStyle) { super.init(style: style) // Custom initialization } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // #pragma mark - Table view data source override func numberOfSectionsInTableView(tableView: UITableView?) -> Int { return 1 } override func tableView(tableView: UITableView?,numberOfRowsInSection section: Int) -> Int { return 5 } override func tableView(tableView: UITableView?,cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { var cell = tableView?.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell if !cell { cell = UITableViewCell(style: UITableViewCellStyle.Value1,reuseIdentifier: "Cell") } cell!.textLabel.text = "test" return cell } } 并且appDelegate看起来像这样: func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainViewController: UITableViewController = MainViewController(style: UITableViewStyle.Plain) let navigationController: UINavigationController = UINavigationController() navigationController.pushViewController(mainViewController,animated: false) self.window!.rootViewController = navigationController self.window!.backgroundColor = UIColor.whiteColor() self.window!.makeKeyAndVisible() return true } 该程序运行,但一旦它运行,我收到以下错误: fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'HelloWorld.MainViewController' 然后我将MainViewController(样式:UITableViewStyle.Plain)更改为MainViewController(nibName:nil,bundle:nil),但后来我得到以下语法错误:调用中的额外参数’bundle’ 任何帮助将非常感激
我正在使用UITableViewController的子类,没有问题,使用(nibName:bundle :)形式,但我在我的子类中重写了它.我尝试用标准的UITableViewController替换我的子类,它仍然工作正常.您是否可能覆盖子类中的init(…)方法?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |