您的位置:首页 > 移动开发 > Swift

学习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.

    }

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: