微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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


先写一个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里面去看一下相关的代码转换

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐