Swift在UI中的应用

在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

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,232评论 4 61
  • There have all kinds of colors in nature. Usually, the re...
    粒粒CC阅读 598评论 0 0
  • 草莽英雄阅读 163评论 0 0
  • 今天我休息,天高正爽,阳光正足,连天气也配合着我的心情,闷闷的,燥燥的。骑上自行车出去走走吧,让这郁郁的心情晒晒太...
    斜阳斜阅读 198评论 7 5
  • 5点钟左右,醒了 口渴,但是不想起,想着老公昨天喝的有点高,就起床去厨房烧了一壶水。 趁着水开的那瞬间,开始把大米...
    谷喵儿阅读 165评论 0 0