Swift - 加载XIB 文件

Swift 语言是大趋势 快速开发也是苹果公司推崇的一种开发方式 

XIB 和 StoryBoard 是快速开发的一种方式   

我们可以结合 这几种方式快速开发我们的项目 

但是 StoryBoard 团队开发不建议使用 因为会出现很多问题及冲突 解决起来很麻烦  

下面介绍一下 Swift 是如何加载XIB 文件的 

我们了解OC  加载XIB 文件需要NIB 注册  

在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, width: 375, 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, width: 375, 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.
    }
    
    
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值