Swift 语言是大趋势 快速开发也是苹果公司推崇的一种开发方式
XIB 和 StoryBoard 是快速开发的一种方式
我们可以结合 这几种方式快速开发我们的项目
但是StoryBoard 团队开发不建议使用 因为会出现很多问题及冲突 解决起来很麻烦
下面介绍一下 Swift 是如何加载XIB 文件的
在Swift 中也少不了 步骤和OC 中的一样 就是一些语法格式变了而已
import UIKit class TableViewCell: UITableViewCell { @IBOutlet weak var segmentSelect: UISegmentedControl! @IBOutlet weak var messageLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code self.contentView.backgroundColor = UIColor.yellowColor() } override func setSelected(selected: Bool,animated: Bool) { super.setSelected(selected,animated: animated) // Configure the view for the selected state } }
import UIKit import Foundation class MainViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{ private let cellName = "TableViewCell" @IBOutlet weak var tableView: UITableView! var clickCount: Int = 0 // @IBOutlet weak var tableView: UITableView! //mark - Life_Circle override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor() self.title = "测试" prepareUI() // Do any additional setup after loading the view,typically from a nib. } func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int { return 20; } func btnClick (button :UIButton) { NSLog("点击") } private func prepareUI() { self.view.addSubview(myLabel) self.view.addSubview(buyButton) self.view.addSubview(myButton) //Nib注册 self.tableView.registerNib(UINib(nibName: cellName,bundle: nil),forCellReuseIdentifier: "123") self.tableView.delegate = self self.tableView.dataSource = self //注册cell // tableView.registerClass(TableViewCell.self,forCellReuseIdentifier: cellName) } lazy var myLabel: UILabel = { //color 是常量 let color = UIColor.redColor() //self.view.backgroundColor = color let rect = CGRect(x: 0,y: 100,width: 375,height: 50) let myLabel = UILabel() //mark - myLabel.frame = rect myLabel.text = "百度" myLabel.backgroundColor = UIColor.greenColor() myLabel.textAlignment = NSTextAlignment.Center self.view.addSubview(myLabel) return myLabel }() lazy var myButton:UIButton = { let myButton = UIButton() myButton .setTitle("腾讯",forState: UIControlState.Normal) myButton.backgroundColor = UIColor.cyanColor() myButton.frame = CGRect(x: 0,y: 160,height: 50) myButton .addTarget(self,action: "btnClick:",forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(myButton) return myButton }() lazy var buyButton: UIButton = { let buyButton = UIButton() buyButton.frame = CGRect(x: 0,y: 220,height: 50) buyButton.setTitle("Objective-C",forState: UIControlState.Normal) buyButton.titleLabel?.textAlignment = NSTextAlignment.Center buyButton .addTarget(self,action: "btnClick",forControlEvents: UIControlEvents.TouchUpInside) buyButton.backgroundColor = UIColor.purpleColor() return buyButton }() func btnClick()->Void { NSLog("买东西") let subVC = SubViewController() // public func pushViewController(viewController: UIViewController,animated: Bool) self.navigationController?.pushViewController(subVC,animated: true) } //mark - 懒加载 /* lazy var buyButton: UIButton = { let buyButton = UIButton(type: UIButtonType.Custom) buyButton.setTitle("付款",forState: UIControlState.Normal) buyButton.setBackgroundImage(UIImage(named: "button_cart_add"),forState: UIControlState.Normal) buyButton.layer.cornerRadius = 15 buyButton.layer.masksToBounds = true return buyButton }() */ func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // let cell = tableView.dequeueReusableCellWithIdentifier(cellName) /* var cell = tableView.dequeueReusableCellWithIdentifier(cellName);UITableViewCell?() if (cell == nil) { cell = UITableViewCell(style: .Default,reuseIdentifier: cellName) } cell!.textLabel?.text = "test" */ let cell:TableViewCell = tableView.dequeueReusableCellWithIdentifier("123",forIndexPath: indexPath) as! TableViewCell cell.messageLabel.text = "123"; print(cell.messageLabel.text) cell.accessoryType = UITableViewCellAccessoryType.Checkmark cell.accessoryType = UITableViewCellAccessoryType.DetailButton cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator return cell } func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 100 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }