swift簡單代碼記錄

發現會OC的 看swift還是比較簡單的,只是不用分號很不習慣,僅此記錄下學習時候寫的簡單代碼

創建按鈕

let button = UIButton(type: .custom)

button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)

view.addSubview(button)

button.backgroundColor = UIColor.blue

button .addTarget(self, action: #selector(click), for: .touchUpInside)

實現Button點擊方法

func click() -> () {

}

創建imageView

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))

imageView.backgroundColor = UIColor.red

imageView.image = UIImage(named: "timg.jpeg")

view .addSubview(imageView)

創建tableView

let tab = UITableView(frame:view.frame, style: .plain)

tab.delegate = self

tab.dataSource = self

view.addSubview(tab)

tab.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

//MARK: delegate

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 10

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView .dequeueReusableCell(withIdentifier: "cell", for: indexPath)

cell.textLabel?.text = "\(indexPath.row)"

cell.selectionStyle = .none

cell.contentView.backgroundColor = UIColor.brown

return cell

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return 44

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

print("\(indexPath.row)")

}

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

推薦閱讀更多精彩內容