在UI中swift不是用init初始化 而是用() 屬性都是"."出來的
例子:懶加載一個button
lazy var btn:UIButton = {
let button = UIButton(frame: CGRectMake(70,70,50,50))
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: "btnAction:", forControlEvents: UIControlEvents.TouchUpInside)//button的點擊方法名除了雙引號表示的 還有 Selector("btnAction:") 和 #selector(btnAction:) 這兩種
return button
}()
button的點擊事件方法的實現:進行界面跳轉
func btnAction(btn:UIButton){
let secondVC = SecondViewController()
navigationController?.pushViewController(secondVC, animated: true)
}
懶加載一個label
lazy var label:UILabel = {
let temp = UILabel(frame: CGRectMake(70,150,90,30))
temp.backgroundColor = UIColor.cyanColor()
return temp
}()
用swift寫tableview的時候 代理寫的地方有點不同
//寫在類的外面
//extension 本類名:協議名{}
例子:
extension ViewController:UITableViewDelegate,UITableViewDataSource{
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tab.dequeueReusableCellWithIdentifier("ss", forIndexPath: indexPath) as! ContactCell//強轉 我這邊自定義了一個cell 并且定義了一個聯系人類 又定義了一個單利 并且把聯系人添加到數組中
let contact = ContactManager.shareContactManager.contactArray[indexPath.row]
cell.cellWithContact(contact)//在自定義cell中寫了一個方法 來將聯系人中的信息賦值給cell中的控件
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ContactManager.shareContactManager.contactArray.count
}
}