首页 > 编程语言 > 详细

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

时间:2015-07-15 23:11:17      阅读:261      评论:0      收藏:0      [点我收藏+]


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

 
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSURLConnectionDataDelegate {
    
    var dataArray = NSMutableArray()
    var tableView: UITableView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.title = "swh"
        
        for var i = 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!)
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentify = "myCellIdentify"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentify) as? UITableViewCell
        if (cell == nil) {
            cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentify)
        }
        var string = self.dataArray.objectAtIndex(indexPath.row) as? String
        cell?.textLabel?.text = string
        
        return cell!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


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

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


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

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


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


本文出自 “东软iOS校友群的技术博客” 博客,请务必保留此出处http://neusoftios.blog.51cto.com/9977509/1674955

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

原文:http://neusoftios.blog.51cto.com/9977509/1674955

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!