加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift篇第五期:UITableView,OC与Swift互调

发布时间:2020-12-14 02:01:09 所属栏目:百科 来源:网络整理
导读:先写一个UITableView的简单创建吧,经过前面几期的内容,那么创建一个常用的控件也是蛮简单的哦 classViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,NSURLConnectionDataDelegate{vardataArray=NSMutableArray()vartableView:


先写一个UITableView的简单创建吧,经过前面几期的内容,那么创建一个常用的控件也是蛮简单的哦

classViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,NSURLConnectionDataDelegate{

vardataArray=NSMutableArray()
vartableView:UITableView?

overridefuncviewDidLoad(){
super.viewDidLoad()
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
self.title="swh"

forvari=0;i<6;i++{
self.dataArray.addObject("row(i)")
}

self.tableView=UITableView(frame:self.view.bounds,style:.Plain)
self.tableView!.delegate=self
self.tableView!.dataSource=self
self.view.addSubview(self.tableView!)
}

functableView(tableView:UITableView,numberOfRowsInSectionsection:Int)->Int{
returnself.dataArray.count
}

functableView(tableView:UITableView,cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell{
letcellIdentify="myCellIdentify"
varcell=tableView.dequeueReusableCellWithIdentifier(cellIdentify)as?UITableViewCell
if(cell==nil){
cell=UITableViewCell(style:.Default,reuseIdentifier:cellIdentify)
}
varstring=self.dataArray.objectAtIndex(indexPath.row)as?String
cell?.textLabel?.text=string

returncell!
}

functableView(tableView:UITableView,didSelectRowAtIndexPathindexPath:NSIndexPath){

}

overridefuncdidReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//DispoSEOfanyresourcesthatcanberecreated.
}


}


然后就是在Swift里面调用O-C代码,这样有利于我们可以利用很多O-C的三方开源库哦

我们在工程中新创建一个OC类文件,它会提示是否建立与Swift的桥接,选择YES后,就会新创建一个文件,名字是“工程名-Bridging-Header.h”的文件,在里面导入你想要调用的O-C头文件就可以了哦


然后是介绍O-C调用Swift代码,感觉这个并不是很常用哦

就是直接导入头文件,名字是“工程名-Swift.h”,当然了,名字不一定正确,我们可以去看看设置里面相关的product Module Name,然后替换工程名字就可以了哦


好啦,基本就是这些吧,其实我们可以在swift.h里面去看一下相关的代码转换哦

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读