我正在使用 Swift 2.0和Xcode 7.2. 我想学习如何制作没有故事板的应用程序(带有纯编程代码的UI).首先,我试图在一个自定义UITableView单元格中创建一个带有三个标签的简单应用程序,该单元格
我想学习如何制作没有故事板的应用程序(带有纯编程代码的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.
}
}
我不知道如何以编程方式创建自定义单元格,我可以添加对象.
帮助将不胜感激.
谢谢.
如果你没有使用故事板,你可以在你的ViewController所在的类上面定义你的单元格,你的表格就像myCell那样是你的自定义UITableViewCell,如下所示.在这个myCell中,您可以根据需要添加任意数量的对象,并在setUpCell()块中进行设置.
完整代码如下,请确保在cellForRowAtIndexPath中使用单元格时调用setUpCell().
ViewController.swift
import #UIKit
class myCell: UITableViewCell {
// Define label, textField etc
var aMap: UILabel!
// Setup your objects
func setUpCell() {
aMap = UILabel(frame: CGRectMake(0, 0, 200, 50))
self.contentView.addSubview(aMap)
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
// for ex, lets say, your data array is defined in the variable below
var dataArray = [[String:AnyObject]]() //Array of your data to be displayed
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()
// register your class with cell identifier
self.tableView.registerClass(myCell.self as AnyClass, forCellReuseIdentifier: "Cell")
self.view.addSubview(tableView)
dataArray = // Something loaded from internet
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return flightDataArr.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath)
var cell:myCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? myCell
if cell == nil {
cell = myCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
var data = dataArray[indexPath.row]
cell?.setUpCell()
cell!.aMap.text = String(dict["productName"])
return cell!
}
看看这是否适合你.我从未使用编程来创建tableView,因此这可能不是以编程方式创建tableView的最佳方式.如果可能的话,我希望其他人可以帮助你找到更好的答案.
