
import UIKitclass DataSource: NSObject,UItableVIEwDataSource,UItableVIEwDelegate { var movIEs = [String]() //MARK: - UItableVIEwDataSource func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int { return 1 } func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { return movIEs.count } func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { let cell = tableVIEw.dequeueReusableCellWithIDentifIEr("cellIDentifIEr",forIndexPath: indexPath) as UItableVIEwCell cell.textLabel?.text = movIEs[indexPath.row] return cell }} 我的问题是:如果我想在DataSource类中使用属性或调用vIEwController类的方法,我该怎么办?例如,我想在用户选择单元格时调用presentVIEwController:
func tableVIEw(collectionVIEw: UICollectionVIEw,dIDSelectItemAtIndexPath indexPath: NSIndexPath) { //do something here presentVIEwController(vIEwController!,animated: true,completion: nil)}解决方法 您可以处理选定的回调: class DataSource: NSObject,UItableVIEwDelegate { var movIEs = [String]() private var selectedCallback = ((NSIndexPath)->VoID)? func selectedItemAtIndex(callback:(NSIndexPath) -> VoID) { selectedCallback = callback }} 打回来:
func tableVIEw(collectionVIEw: UICollectionVIEw,dIDSelectItemAtIndexPath indexPath: NSIndexPath) { //do something here if let callback = selectedCallback { callback(indexPath) }} 使用:
dataSource.selectedItemAtIndex() { [weak self] indexPath in // do something ... // presentVIEwController(vIEwController!,completion: nil)} 总结 以上是内存溢出为你收集整理的ios – 如何从Swift中的viewController类中分离dataSource和delegate?全部内容,希望文章能够帮你解决ios – 如何从Swift中的viewController类中分离dataSource和delegate?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)