Swift (閉包返回值)

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

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容