swift3.0已經(jīng)到來,2.3的項(xiàng)目一運(yùn)行遥皂,崩潰個(gè)百八個(gè)報(bào)錯(cuò)是相當(dāng)正常的逗堵。
還是來進(jìn)入Swift3.0的大坑中吧秉氧,不一定要用在實(shí)際的項(xiàng)目中去,但是不學(xué)習(xí)是不好的砸捏,來學(xué)起吧-谬运。-
3.0變化了很多的地方隙赁,最基本的創(chuàng)建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 {
//設(shè)置單元格數(shù)據(jù)
// var cell = tableView.dequeueReusableCell(withIdentifier: "CELL")
// if cell == nil {
// cell = UITableViewCell(style: .default, reuseIdentifier: "CELL")
// }
//
// cell?.textLabel?.text = "let's fucking"
//cell標(biāo)志符垦藏,使cell能夠重用(如果不需要重用,是不是可以有更簡單的配置方法伞访?)
let indentifier:String = "cell"
//注冊自定義cell到tableview中掂骏,并設(shè)置cell標(biāo)識符為indentifier(nibName對應(yīng)UItableviewcell xib的名字)
let nib:UINib = UINib(nibName:"TableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: indentifier)
//從tableview中獲取標(biāo)識符為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)
}
}
結(jié)語:語法上很多變化厚掷,但是跟OC是很相似的弟灼,沒事可以學(xué)學(xué)级解,以備以后穩(wěn)定了上手-。0
Simulator Screen Shot 2016年9月27日 21.20.38.png