Nil*_*ne- 7 uitableview programmatically-created swift ios8
正如标题所说,我试图以编程方式设置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
}
}
Run Code Online (Sandbox Code Playgroud)
并且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
}
Run Code Online (Sandbox Code Playgroud)
该程序运行,但一旦它运行,我收到以下错误:
fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'HelloWorld.MainViewController'
Run Code Online (Sandbox Code Playgroud)
我然后更改MainViewController(style: UITableViewStyle.Plain)为MainViewController(nibName: nil, bundle: nil)但然后我得到以下语法错误:Extra argument 'bundle' in call
任何帮助将非常感激
我使用 UITableViewController 的子类,没有任何问题,使用 (nibName:bundle:) 形式,但我已经在我的子类中覆盖了它。我尝试用标准 UITableViewController 替换我的子类,它仍然工作正常。您是否可能重写子类中的 init(...) 方法?
| 归档时间: |
|
| 查看次数: |
11373 次 |
| 最近记录: |