swift开发

苹果在2014年开发者大会上发布了swift语言,随后引起了开发者的高度关注,大有取代oc之意,现在随着swift语言的越发成熟,自己也在没事的时候研究一下,不能被时代所淘汰,嘿嘿。

对比oc语言,自己也觉得有点不同,说明如下:

1:swift没有main函数,相比oc没有把它作为函数的入口类进行执行。

2;swift的文件后缀为.swift,没有.h和.m之分。

3:没有分号,这对很多的开发者来说想必是一种福利吧。

4:他们的常量和变量有类似reactive  native的写法;

还有很多的不同之处,需要我们去发现,探索,最后附上自己写的控件,供大家参考,也随时欢迎大家的批评指正。

 

import UIKit

class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

    override func viewDidLoad() {
        super.viewDidLoad()
        print("hello, world")
       
        var imageView = UIImageView()
        imageView = UIImageView()
        imageView.frame = CGRectMake(100, 250, 50, 50)
        imageView.image = UIImage(named:"");
        imageView.backgroundColor = UIColor.redColor()
        self.view.addSubview(imageView)
        
        let  mytableview = UITableView(frame: CGRectMake(0, 0, 400, 400), style: UITableViewStyle.Plain)
        self.view.addSubview(mytableview)
        mytableview.delegate = self;
        mytableview.dataSource = self;
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }
    func  tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let  cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: nil)
        cell.textLabel?.text = "我爱你"
        cell.detailTextLabel?.text = "gaga"
        //每一行有一个下载按钮
        let btn:UIButton = UIButton(type: UIButtonType.Custom)
        btn.frame = CGRectMake(UIScreen.mainScreen().bounds.width-100, 10, 80, 50)
        btn.setTitle("下载", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.backgroundColor = UIColor.redColor()
        btn.tag = indexPath.row
        btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        cell.contentView.addSubview(btn)
        return  cell
        
        
    }
    func btnClick(sender:UIButton){
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

 

posted @ 2016-06-03 15:22  qinxiaoguang  阅读(201)  评论(0编辑  收藏  举报