swift初级篇

目录

UITableView swift版本的使用方法(附Demo)
  1、UITableView相关协议及实现
  2、如何让swift版本的TableView与xib绘制的Cell相关联
StoryBoard使用控制器进行转场传值
  1、通过Identifier控制专场
  2、通过as!强转控制器进行传值
Swfit对比OC新增常用属性
  1、as!与as?
  2、override
  3、闭包
  4、数组字典常用方法
Swfit对比OC优点何在~

正文

UITableView swift版本的使用方法(附Demo)

1、swift版本的TableView与OC中TableView的协议方法类似、只是在写法上更简洁化
OC版本代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.friendSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return cell;
}

Swift版本代码

// MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return  areas.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)as!CustomTableViewCell
        /*赋值操作*/
        cell.nameLabel?.text = areas[indexPath.row]
        cell.detailLabel.text = areas[indexPath.row]
        cell.fontLabel.text = areas[indexPath.row]
        
        cell.headImageView?.image = UIImage(named: areasPic[indexPath.row])
        /*判断cell的点击状态*/
        if visited[indexPath.row] {
            cell.accessoryType = .checkmark
        }else{
            cell.accessoryType = .none
        }
        /*简洁写法 三元表达式*/
//        cell.accessoryType = self.visited[indexPath.row] ?.checkmark:.none
        return cell
    }
    

备注:到这你会发现其实两个语言在实现代理上基本相同只是写法不同而已,还有很多代理方法都基本相同就不一一列举.

2、如何让swift版本的TableView与Cell相关联

1、创建一个swift版本的cell

2、将控制器上的cell的Class设置为创建的cell的名字


设置cell的class

3、给cell一个专属的Identifier


给cell一个专属的Identifier

4、在TableView协议中初始化cell

/*as!强转为你创建的cell*/
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)as!CustomTableViewCell

StoryBoard使用控制器进行转场传值

1、在StoryBoard中推拽一个控制器(UIViewController)
2、通过右键关联的控制器(选择跳转模式:show 、push、或者自定义专场等 )
3、给跳转segue一个专属的Identifier


给跳转segue一个专属的Identifier

4、实现跳转并传值

1、需要在跳转的控制器当中声明属性值areaName
2、
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        /*
         1、确认是由指定的segue触发
         2、获取转场的目标控制器、并转换成具体的类
         3、设置 目标控制器的属性
         */
        if segue.identifier == "showAreaDetail"{
            let dest = segue.destination as!AreaDetailViewController
            dest.areaName = areasPic[tableView.indexPathForSelectedRow!.row]
        }
    }
Swfit对比OC新增常用属性

1、as!与as?

as 类型转换,从一个类型转换到另一个类型
as!强制转换(失败app会崩溃)
as?安全转换(失败不会崩溃)

2、override

override 在 func之前的意思 是子类要使用父类的方法就需要使用override 如果不是则可以选择不需要~

3、闭包

通常使用在函数当中有函数的情况,大大减轻了代码量(有意想不到的惊喜)

4、数组字典常用方法

集合

let cardno : Set = ["1","3","5"]
        
        var citys : Set = ["shanghai","beijing","hunan","hefei","jilin"]

        /*元素个数*/
        citys.count
        /*是否为nil*/
        cardno.isEmpty
        /*插入:insert*/
        citys.insert("guangzhou")
        /*移除*/
        citys.remove("shanghai")
        /*是否包含某个元素*/
        citys.contains("beijing")
        /*set 转化为数组*/
        let cityArr = citys.sorted()
        /*交际*/
        var x :Set = [1,3,2,4]
        let y :Set = [5,6,3,4]
        x.intersection(y)
        /*差际*/
        x.subtract(y)
        /*并际*/
        x.union(y)
        /*补际*/
        x.symmetricDifference(y)
        /*相等的元素*/
        x == y
        /*子集 isSubet(可以相等) 严格子集 isStrictSubset*/
        x.isSubset(of: y)
        x.isStrictSubset(of: y)
        /*父集 isSuperSet 严格父集 isStrictSuperSetOf*/
        x.isSuperset(of: y)
        x.isStrictSuperset(of: y)
        /*无交集*/
        let j :Set = ["动漫","游戏"]
        let k :Set = ["购物","小吃","化妆"]
        j.isDisjoint(with: k)
/*字典*/
        var airPorts = ["PVG":"shanghai","CHU":"dalian","DUB":"dublin AirPoart"]
        /*字典个数*/
        print(airPorts.count)
        /*是否为空*/
        print(airPorts.isEmpty)
        /*字典 添加 元素*/
        airPorts["SHQ"] = "HongQiao Airport "
        airPorts["CHU"] = "大连周子水机场"
        /*获取*/
//        airPorts["PVG"]
        /*移除*/
        airPorts["DUB"] = nil
        /*遍历*/
        for (key,value) in airPorts {
            print(key,value)
        }
        /*遍历只打印key&value*/
        for key in airPorts.keys {
            print(key)
        }
        /*把key和value单独提取出来*/
        let codes = [String](airPorts.keys)
        let AirPoartName = [String](airPorts.values)
        print(codes)
        print(AirPoartName)
Swfit对比OC优点何在~

1、写法简洁!!!!
2、更易懂!!!!
3、更直接!!!!
总之~ 用了swift之后你会淡忘OC的(有种换了一个更好对象的感觉~哈哈哈哈😂)

附:Demo传送门

我是辛小二~喜欢记得点赞哦~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容