学习swift《创建简单的控件》

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值