swift 幾個比較好的UI庫
1.tableView的代理方法
在swift中代理變得更加重要,當(dāng)在繼承代理的時候,代理的require方法必須實現(xiàn),否則直接就報錯先誉。
但是這個報錯一點也不友好,下面這個例子就是先寫了代理的烁,還沒有實現(xiàn)其代理方法褐耳,引入的時候直接報錯了.But,在Xcode 9版本修復(fù)這個問題渴庆,錯誤提示很友好了铃芦。
這報錯提示“Type 'UIViewController' does not conform to protocol 'UITableViewDataSource'” 簡直一臉懵逼啊,查了好久才知道原來是代理方法沒有寫襟雷,寫了其require方法就好了刃滓。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = "row + \(indexPath)"
return cell
}
2.如果定義tableView是grouped類型,默認(rèn)是有sectionHeader 和 sectionFooter的耸弄,即使你沒有實現(xiàn)咧虎,也是有一個灰色View在上面。如圖
這時候计呈,你不想要header或是footerView砰诵,
1.將tableView定義成plain類型,默認(rèn)就沒有header和footerView了
2.實現(xiàn)header或footerView的高度代理方法捌显,設(shè)置一個極小值即可
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.0001
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.0001
}
3.tableView 注冊cell方法
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
或者
collectionView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
暫不知道什么區(qū)別茁彭,都可以好像。 自定義cell注冊的時候好像就要用self了扶歪。
Swift懶加載
需要的時候初始化內(nèi)存理肺,對內(nèi)存開銷較小,節(jié)省內(nèi)部資源
代碼初始化放在一起善镰,代碼塊比較好劃分妹萨,方便別人和自己閱讀
它本質(zhì)在siwft中確實是一個閉包,執(zhí)行順序是這樣的媳禁,如果這個lazy修飾的變量沒值眠副,就會執(zhí)行閉包中的東西,不是每次都執(zhí)行(本人補(bǔ)充:這也就是為什么在Swift中的懶加載沒有oc中判斷竣稽。if(xx==nil){初始化xx}的代碼段)囱怕。
lazy var label: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
label.text = "Test"
label.font = UIFont.systemFont(ofSize: 14.0)
label.textColor = UIColor.red
return label
}()
swift中對基本知識的要求更高了,基本知識必須掌握牢毫别。
因為這里對了好多安全機(jī)制娃弓,有些異常不會報錯,但是就是不會出你要的結(jié)果岛宦,很難排查台丛,所以要有更好的基礎(chǔ)知識。
遇見的小坑,以后慢慢填坑挽霉。防嗡。