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)")

}

你可能感兴趣的:(swift简单代码记录)