1.懒加载
private lazy var label : UILabel = {
let label = UILabel()
label.textColor = UIColor.blackColor()
label.textAlignment = NSTextAlignment.Center
label.font = UIFont.systemFontOfSize(18)
return label
}()
2.重写model的set方法赋值(可选类型要先判断为不为nil,为nil强转会崩)
var model : WBModel? {
didSet {
label.text = model!.title
//如果拼接的话要转
if let string = model?.title {
label.text = "cell \(string)"
}
}
}
3.按钮的点击 :
无参数Selector("btnWithLogin”) 或者 #selector(LoginViewController.btnWithLogin)
有参数Selector(“btnWithLogin:”)
#selector(MyDetailHeardView.btnWithLogin(_:))
btn_sure.addTarget(self, action: Selector(“btnWithLogin:"), forControlEvents: UIControlEvents.TouchUpInside)
注意:@objc别忘了这个!!!!!!
@objc private func btnWithLogin(btn : UIButton) -> Void {
if delegate != nil {
delegate?.myDetailHeardViewDidClick(btn.tag)
}
}
4.代理delegate
1.定义
protocol LoginInputViewDelegate : NSObjectProtocol {
func didClickWithForget()
}
class LoginInputView: UIView {
weak var delegate : LoginInputViewDelegate?
//MARK: - 点击了忘记密码,别忘了@objc
@objc private func btnWithForget() -> Void {
if delegate != nil {
delegate?.didClickWithForget()
}
}
private lazy var btn_forget : UIButton = {
let btn_forget = UIButton(type: UIButtonType.Custom)
btn_forget.addTarget(self, action: #selector(LoginInputView.btnWithForget), forControlEvents: UIControlEvents.TouchUpInside)
return btn_forget
}()
}
2.实现
extension LoginViewController : LoginInputViewDelegate {
func didClickWithForget() {
print("点击了忘记密码")
}
}
5.重写frame的set方法,注意:如果头部要出现空隙,y+的同时,height要-
//重写frame
override var frame:CGRect{
didSet {
var newFrame = frame
newFrame.origin.x += 10/2
newFrame.size.width -= 10
newFrame.origin.y += 10
newFrame.size.height -= 10
super.frame = newFrame
}
}
- 移除通知
deinit
{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
7.下载图片dispatch_group_create用法
private func cachesImages(viewModels: [StatusViewModel])
{
// 0.创建一个组
let group = dispatch_group_create()
for viewModel in viewModels
{
// 1.从模型中取出配图数组
guard let picurls = viewModel.thumbnail_pic else
{
// 如果当前微博没有配图就跳过, 继续下载下一个模型的
continue
}
// 2.遍历配图数组下载图片
for url in picurls
{
// 将当前的下载操作添加到组中
dispatch_group_enter(group)
// 3.3利用SDWebImage下载图片
SDWebImageManager.sharedManager().downloadImageWithURL(url, options: SDWebImageOptions(rawValue: 0), progress: nil, completed: { (image, error, _, _, _) -> Void in
NJLog("图片下载完成")
// 将当前下载操作从组中移除
dispatch_group_leave(group)
})
}
}
// 监听下载操作
dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
NJLog("全部下载完成")
self.statuses = viewModels
}
}