我正在使用Swift 2.0和Xcode 7.2。
我想学习如何制作一个没有storyboard的应用程序(纯编程代码UI)。首先,我正在尝试制作一个简单的应用程序,在自定义的UITableView单元格中有三个标签,这些标签将通过互联网动态更新。
到目前为止,我已经完成了以下内容:
- 创建了一个新的Swift项目,并从项目中删除了main.storyboard - 添加了一个视图控制器作为AppDelegate中的rootViewController - 包含代码以在此视图内创建UITableView
以下是我想要完成的其他任务(全部以编程方式完成,而不使用属性检查器):
- 将UINavigationController插入ViewController中 - 添加具有三个标签的自定义单元格 - 更新表视图数据
如果可能的话,我希望拥有一切都能在横向模式下工作的能力。
有人可以告诉我如何做到这一点吗?
AppDelegate.swift
我想学习如何制作一个没有storyboard的应用程序(纯编程代码UI)。首先,我正在尝试制作一个简单的应用程序,在自定义的UITableView单元格中有三个标签,这些标签将通过互联网动态更新。
到目前为止,我已经完成了以下内容:
- 创建了一个新的Swift项目,并从项目中删除了main.storyboard - 添加了一个视图控制器作为AppDelegate中的rootViewController - 包含代码以在此视图内创建UITableView
以下是我想要完成的其他任务(全部以编程方式完成,而不使用属性检查器):
- 将UINavigationController插入ViewController中 - 添加具有三个标签的自定义单元格 - 更新表视图数据
如果可能的话,我希望拥有一切都能在横向模式下工作的能力。
有人可以告诉我如何做到这一点吗?
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.backgroundColor = UIColor.whiteColor()
window!.rootViewController = ViewController()
window!.makeKeyAndVisible()
return true
}
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.whiteColor()
tableView.frame = CGRectMake(0 , 0, self.view.bounds.width, self.view.bounds.height)//Optional for table size
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "myIdentifier")
myCell.textLabel?.text = "\(indexPath.row)"
myCell.detailTextLabel?.text = "Subtitle"
return myCell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我不知道如何通过编程创建自定义单元格并添加对象。
希望能得到帮助。
谢谢。
}
我是一个相当新手,总是使用storyboard。您能否编辑掉所有与自定义数据相关的内容,并使其更与问题相关。这将非常有帮助。我真的很感激您的努力。 - Vicky Arora