【发布时间】:2016-01-19 12:59:21
【问题描述】:
我正在使用 Swift 2.0 和 Xcode 7.2。
我想学习如何制作没有情节提要的应用(带有纯编程代码的 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.
}
}
我不知道如何以编程方式创建一个可以添加对象的自定义单元格。
我们将不胜感激。
谢谢。
【问题讨论】:
标签: ios swift uitableview