Swift (闭包返回值)

2016-07-24  本文已影响53人  Coffee丶

importUIKit

classViewController:UIViewController{

overridefuncviewDidLoad() {

super.viewDidLoad()

/*

let sc = UIScrollView(frame: CGRect(x: 0, y: 100, width: 375, height: 44))

sc.backgroundColor = UIColor.redColor()

let count = 15

let width = 50

for i in 0..

let label = UILabel()

label.backgroundColor = UIColor.greenColor()

label.textColor = UIColor.darkGrayColor()

label.font = UIFont.systemFontOfSize(17)

label.text = "text\(i)"

label.frame = CGRect(x: i * width, y: 0, width: width, height: 44)

sc.addSubview(label)

}

sc.contentSize = CGSize(width: count * width, height: 44)

view.addSubview(sc)

*/

letsc =createScrollView({ () ->Intin

return15

}) { (index) ->UILabelin

letwidth =50

letlabel =UILabel()

label.backgroundColor=UIColor.greenColor()

label.textColor=UIColor.darkGrayColor()

label.font=UIFont.systemFontOfSize(17)

label.text="text\(index)"

label.frame=CGRect(x: index * width, y:0, width: width, height:44)

returnlabel

}

view.addSubview(sc)

}

funccreateScrollView(labelCount: ()->Int, labelWithIndex: (index:Int)->UILabel) ->UIScrollView{

// 1.创建UIScrollView

letsc =UIScrollView(frame:CGRect(x:0, y:100, width:375, height:44))

letcount = labelCount()

//let width = 50

// 2.遍历创建UILabel

foriin0..

/*

let label = UILabel()

label.backgroundColor = UIColor.greenColor()

label.textColor = UIColor.darkGrayColor()

label.font = UIFont.systemFontOfSize(17)

label.text = "text\(i)"

label.frame = CGRect(x: i * width, y: 0, width: width, height: 44)

sc.addSubview(label)

*/

letlabel = labelWithIndex(index: i)

sc.addSubview(label)

sc.contentSize=CGSize(width:CGFloat(count) * label.bounds.width, height:44)

}

//返回UIScrollView

returnsc

}

}

上一篇 下一篇

猜你喜欢

热点阅读