0

I am extreeemely new to Swift and native iOS developement.

I was succesfully able to create a label in Swift and it showing in my app like this:

import UIKit
import SpreadsheetView

class SampleView: UIView {

  override init(frame: CGRect) {
    super.init(frame: frame)

    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    label.text = "This is Swift"
    self.addSubview(label)

  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

However I now need to add a SpreadsheetView - from here - https://github.com/kishikawakatsumi/SpreadsheetView

Instead of adding a label my code changes to this:

import UIKit
import SpreadsheetView

class ViewController: UIViewController, SpreadsheetViewDataSource {
  @IBOutlet weak var spreadsheetView: SpreadsheetView!

  override func viewDidLoad() {
    super.viewDidLoad()
    spreadsheetView.dataSource = self
  }

  func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int {
    return 200
  }

  func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int {
    return 400
  }

  func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat {
    return 80
  }

  func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat {
    return 40
  }
}

class SampleView: UIView {

  override init(frame: CGRect) {
    super.init(frame: frame)

    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "ViewController");
    self.addSubview(controller.view);
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

However while running I get a SIGABRT, am I going about this the right way. I see lots of articles on adding controllers like this one - Adding View controllers as subview in Swift - but may you please help me with this above case.

Here is my screenshot:

2
  • 1
    Have you set the storyboard identifier to the one you have written in the code ViewController in storyboard ? Commented Jun 22, 2017 at 6:15
  • Hi there @BhautikZiniya - thanks for asking, I did not - I am not sure how to do that. It seemed Sour's method below is the way to go as it fixed the SIGABRT, do you think it is correct? +1 for your question, thank you sir. Commented Jun 22, 2017 at 6:18

2 Answers 2

1

Try this:

let controller = ViewController()
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you sir trying immediately!
Just tested it and it got rid of that error! It is giving me an error of EXC_BAD_INSTRUCTION now withing the ViewController - screenshot: i.imgur.com/8lzXH6y.png - is this something specific to the SpreadsheetView? If it is I will happily accept your answer and look in that direction. Thank you! It might similar to this error - stackoverflow.com/q/27339959/3791822
Thanks Sour for your comment, I just learned of the console, it is showing me this - i.imgur.com/AoGSxu3.png -- also I found some interesting messages here - i.imgur.com/kFhpEwn.png
Very cool shortcut thanks for those baby steps! It seems to be the same content seen in the view above it - i.imgur.com/eZ87Jel.png
I think this is the solution to this question. Thank you very much sir! Combined with the clarification given by @nayem below I was able to get it building! Now I'm just working on giving the controller a frame size, as i think this is why no data is showing on render. Thanks again sir!
1

You don't need your SampleView class. I think you got confused because of that @IBOutlet weak var spreadsheetView: SpreadsheetView!. You don't have to follow this procedure. You just create an instance and use it accordingly.

If you are trying to make your view a SpreadsheetView just use it like below way:

import UIKit
import SpreadsheetView

class ViewController: UIViewController, SpreadsheetViewDataSource {

    let spreadsheetView = SpreadsheetView()  //have an SpreadsheetView

    override func viewDidLoad() {
        super.viewDidLoad()

        spreadsheetView.dataSource = self
        self.view = spreadsheetView  //set your controller's container view as that spreadsheetView
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int {
        return 200
    }

    func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int {
        return 400
    }

    func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat {
        return 80
    }

    func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat {
        return 40
    }

}


Edit: different approach

If you still want to use @IBOutlet approach, see the below image and follow step by step (1-2-3-4) connecting a View with SpreadsheetView

3 Comments

Thank you very much sir! This really helped me understand better! I mixed your solution with Sour's solution. Can I accept two solutions? But I had to add this as a subview of a UIView. I am actually writing a React Native app, and trying to bridge this component to it. I am not getting any errors now! It's just that the contents is not showing. I am thinking the width/height nees a frame like I did for label on line 150 - pastebin.com/1yqmEyVf - would you have idea on how to do set a frame for the ViewController instance?
No, you can't accept two solution at the same time. But you can always change your answer to the best match for your problem. And again if you have other queries other than this specific problem, you have to ask another question @Blagoh
Thanks bro I'll do that. I sincerely appreciate your help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.