Ios 如何将MGSpotyViewController与Swift一起使用?

Ios 如何将MGSpotyViewController与Swift一起使用?,ios,swift,uiviewcontroller,Ios,Swift,Uiviewcontroller,我试图在我的swift项目中使用,但我似乎无法使其工作 我已经通过cocoa pods安装了它,我在故事板上配置了一个ViewController来使用这个类,现在我的代码是这样的: import UIKit import MGSpotyViewController class profileTableViewController: MGSpotyViewController { override func viewDidLoad() { super.viewDidLoad()

我试图在我的swift项目中使用,但我似乎无法使其工作

我已经通过cocoa pods安装了它,我在故事板上配置了一个ViewController来使用这个类,现在我的代码是这样的:

import UIKit
import MGSpotyViewController

class profileTableViewController: MGSpotyViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    overView = myOverView()
}

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

func myOverView() -> UIView{
    let view = UIView(frame: self.overView.bounds)

    let imageView = UIImageView(frame: CGRectMake(view.center.x-50.0, view.center.y-60.0, 100.0, 100.0))
    imageView.contentMode = .ScaleAspectFill
    imageView.clipsToBounds = true
    imageView.image = UIImage(named: "profile")!
    imageView.layer.borderColor = UIColor.whiteColor().CGColor
    imageView.layer.borderWidth = 2.0
    imageView.layer.cornerRadius = imageView.frame.size.width/2.0

    view.addSubview(view)

    return view
}
}
问题是我不知道如何配置委托方法,XCode无法识别它们


有人用swift配置了这个控制器吗

您的view controller应该实现与pod自述相同的数据源和委托方法,例如:

func spotyViewController(_ viewController: MGSpotyViewController, numberOfRowsInSection section: Int) -> Int {
    ...
}
func spotyViewController(_ viewController: MGSpotyViewController, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell? {
    ...
}
func spotyViewController(_ viewController: MGSpotyViewController, heightForHeaderInSection section: NSInteger) -> CGFloat {
    ...
}
func spotyViewController:(_ viewController: MGSpotyViewController, titleForHeaderInSection section: NSInteger) -> String? {
    ...
}
然后在
viewDidLoad
中执行此操作:

self.dataSource = self
self.delegate = self

或者使用故事板/nib将这些连接链接回视图控制器。

谢谢,我认为这将解决与表视图相关的问题。我遇到的另一个问题是,我不知道如何从故事板初始化它,因为文档只说明如何从代码而不是从故事板初始化它。我尝试使用initWithCoder并在其内部调用:super.init(main image:UIImage(名为:“profile”)!),但我得到了以下错误:“致命错误:使用未实现的初始值设定项‘init(main image:tableCrollingType:)‘for class’***.profileTableViewController’”从情节提要、视图控制器初始化什么?不要从
initWithCoder
调用
init
,只能从覆盖的
initWithCoder
调用
super.initWithCoder
。如果您想共享公共init代码,您必须将其放入您自己的方法中,您可以从两个init路径调用该方法。但是,通常将所有视图控制器初始化延迟到
viewDidLoad
。另外,
awakeFromNib
viewDidLoad
之前的某个时间被调用,您可以覆盖该方法并在其中进行一些初始化。您必须在初始化后的某个时间设置映像,在
awakeFromNib
viewDidLoad
中调用
setMainImage
。不要跳过苹果文档中的指南部分,比如“iOS的视图控制器编程指南”,它确实涵盖了一些超出任何特定类参考范围的主题,比如将视图控制器与故事板结合使用。是的,我也这么想,但是在
initWithMainImage
方法中有其他初始化代码如果它只在
initWithMainImage
中执行基本操作,那么它不支持从故事板实例化。要进行双重检查:在情节提要中创建一个非子类化的
MGSpotyViewController
,请使用调试器中的断点查看从未调用
initWithMainImage
。提交问题或自行解决。