学习swift《创建简单的控件》
2017-08-02 14:48
483 查看
学习swift《创建简单的控件》
闲来无事无事于是就学学swift,我先前学的是oc,于是昨天看了一天的swift的语法和结构。以前也看过swift只是没有系统的学过。发现swift和oc语法差别很大,不过看完swift的语法后就深深的喜欢上swift,我觉得swift的语法灵活多变,非常强大。更是被swift1的闭包深深吸引。废话不多说先简单的创建几个简单的控件,我觉得我创建控件的方法有点繁琐,给button添加事件是不是这样写,我不知道对错,谁有简便的创建方法告诉我一下,以后好好交流。
上代码
import UIKit
class ViewController: UIViewController {
//override 重写父类方法需要写加此标记
override func viewDidLoad() {
super.viewDidLoad()
//添加一个label
let label = UILabel();
label.frame = CGRect.init(x: 100, y: 100, width: 100, height: 50);
self.view.addSubview(label)
label.text = "我是label"
label.backgroundColor = UIColor.red;
//添加一个button
let button = UIButton()
button.frame = CGRect.init(x: 100, y: 200, width: 100, height: 50)
button.backgroundColor = UIColor.gray
button.titleLabel?.text = "我是一个button"
button.addTarget(self, action:#selector(ViewController.buttonclick), for:UIControlEvents.touchUpInside)
self.view.addSubview(button)
//添加一个view
let view = UIView();
view.frame = CGRect.init(x: 100, y: 300, width: 200, height: 100)
view.backgroundColor = UIColor.red;
self.view.addSubview(view)
//在view上添加一个label
let vLabe = UILabel();
vLabe.frame = CGRect.init(x: 0, y: 0, width: 200, height: 50)
vLabe.backgroundColor = UIColor.blue
vLabe.text = "我是view上的label"
view.addSubview(vLabe)
}
func buttonclick() {
print("点击了按钮");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
添加一个tableview
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var cellArr:[String] = []
func youbiananniu() {
print("点击右边按钮");
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.gray
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "右边", style: UIBarButtonItemStyle.plain, target: self, action:#selector(youbiananniu))
self.navigationItem.title = "第二页"
for i in 0...40 {
cellArr.append("第\(i)个单元格")
}
//创建个tableview
let table = UITableView.init(frame: CGRect.init(x: 0, y: 64, width: self.view.frame.size.width, height: self.view.frame.size.height), style: UITableViewStyle.plain)
table.delegate = self;
table.dataSource = self;
self.view.addSubview(table)
}
//返回单元格高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
//返回单元格数量
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellArr.count
}
//返回单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell==nil {
cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
}
cell?.selectionStyle = UITableViewCellSelectionStyle.none
cell?.textLabel?.text = cellArr[indexPath.row];
cell?.backgroundColor = UIColor.red
return cell!
}
//点击单元格
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print("点击了\(indexPath.row)");
}
}
添加一个collectionview
import UIKit
class ViewController: UIViewController , UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
let flowLayout = UICollectionViewFlowLayout.init()
flowLayout.minimumLineSpacing = 5
flowLayout.minimumInteritemSpacing = 5
flowLayout.itemSize = CGSize.init(width: 100, height: 100)
let cocleView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: flowLayout)
cocleView.delegate = self
cocleView.dataSource = self
self.view.addSubview(cocleView)
cocleView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1;
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 50
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->UICollectionViewCell
{
var cell:UICollectionViewCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if cell==nil {
cell = UICollectionViewCell()
}
cell?.backgroundColor = UIColor.red
return cell!
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("===点击了单元格\(indexPath.item)");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
相关文章推荐
- Swift学习之函数和简单地控件的创建
- <iOS>swift简单控件的创建
- swift 简单控件的创建
- OCX学习 - 创建简单的ActinveX控件
- 2、swift学习-创建基本的控件
- 为学习APUE(Unix环境高级编程)偷懒,而写的脚本,基本上相当于一个简单的工程创建脚本了
- BI学习之一创建一个简单的Olap多维数据集的展现
- 简单win32程序创建控件
- VS2013MFC单文档工程学习笔记四 - 动态创建多个控件
- Flex权威指南3学习笔记之二------简单控件的使用
- 学习如何用VS2010创建ocx控件
- ios学习--通过xib文件创建子控件 .
- win32sdk学习-简单的创建一个自己的工具栏
- 黑马程序员——IOS学习—基本UI控件的代码创建
- pygame学习(一)创建最简单窗口以及设置背景色
- JasperReport学习笔记2-创建简单的报表例子
- [仅做备份]Openstack 学习记录 (2 Swift 简单尝试SwiftClient)
- WPF学习——制作一个简单的录入界面(2):用C#编程实现所有控件的功能
- Oracle BIEE (Business Intelligence) 11g 11.1.1.6.0 学习(3)创建一个简单的分析
- C#调用存储过程的方法,和简单存储过程创建的方法实例。【转载】为入门的朋友引路,大家互相学习。