Swift3.0起航吧

swift3.0已經到來,2.3的項目一運行,崩潰個百八個報錯是相當正常的。

還是來進入Swift3.0的大坑中吧,不一定要用在實際的項目中去,但是不學習是不好的,來學起吧-。-

3.0變化了很多的地方,最基本的創建UI控件

UIImageView

    //imageview
    let iphoto = UIImageView()
    iphoto.frame = CGRect(x:0,y:0,width:self.view.frame.size.width,height:200)
    iphoto.image = UIImage(named:"zdzz")
    self.view.addSubview(iphoto)

UILabel

//label
    let iLabel = UILabel()
//  iLebel.adjustsFontSizeToFitWidth = true
    iLabel.font = UIFont.systemFont(ofSize: 14)
    iLabel.textColor = UIColor.white
    iLabel.frame = CGRect(x:UIScreen.main.bounds.size.width/2 - 90,y:20,width:180,height:20)
    iLabel.text = "這是從swift3.0開始的label"
//  iLabel.backgroundColor = UIColor.lightGray
    iphoto.addSubview(iLabel)

UIButton

 //swift3.0的button
    let button = UIButton()
    button.frame = CGRect(x:0, y:(screenSize.height-50),width:screenSize.width,height:50)
    button.backgroundColor = UIColor.blue
    button.setImage((UIImage (named: "zixun")), for: .normal)
    button.setTitle("佐羅", for: .normal)
    button.layer.cornerRadius = 15
    button.clipsToBounds = true
    button.addTarget(self, action: #selector(buttontapped), for: .touchUpInside)
    self.view.addSubview(button)

手寫tableview

import UIKit

class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{

var tableView:UITableView! = nil
let screenSize = UIScreen.main.bounds.size
override func viewDidLoad() {
    super.viewDidLoad()
tableView = UITableView()
    tableView.backgroundColor = UIColor.red
    tableView.delegate = self
    tableView.dataSource = self
    tableView.frame = CGRect(x:0,y:200,width:UIScreen.main.bounds.size.width,height:UIScreen.main.bounds.size.height - 200 - 50)
    self.view.addSubview(tableView)
    }
uitableview的代理方法
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return 20;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    //設置單元格數據
//        var cell = tableView.dequeueReusableCell(withIdentifier: "CELL")
   
//        if cell == nil {
//            cell = UITableViewCell(style: .default, reuseIdentifier: "CELL")
//        }
//        
//        cell?.textLabel?.text = "let's fucking"
    
    //cell標志符,使cell能夠重用(如果不需要重用,是不是可以有更簡單的配置方法?)
    let indentifier:String = "cell"
    //注冊自定義cell到tableview中,并設置cell標識符為indentifier(nibName對應UItableviewcell xib的名字)
    let nib:UINib = UINib(nibName:"TableViewCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: indentifier)
    //從tableview中獲取標識符為papercell的cell
    let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: indentifier) as! TableViewCell
    
    return cell
}

func buttontapped(sender: UIButton) {
    print("swift3.0Go~")
}

自定義Cell

上面的代碼中,cellForRowAt中沒有注釋的部分為自定義cell的代碼

import UIKit

class TableViewCell: UITableViewCell {

@IBOutlet weak var cellImage: UIImageView!

@IBOutlet weak var cellLabel: UILabel!
override func awakeFromNib() {
    
    super.awakeFromNib()
    cellImage.layer.cornerRadius = 20;
    cellImage.clipsToBounds = true
    cellLabel.textColor = UIColor.blue
    
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}  
}

結語:語法上很多變化,但是跟OC是很相似的,沒事可以學學,以備以后穩定了上手-。0

Simulator Screen Shot 2016年9月27日 21.20.38.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容