我是Swift和本地iOS开发的新手。
我成功地在Swift中创建了一个标签,它在我的应用程序中显示如下:
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")
}
}
但是我现在需要添加一个SpreadsheetView - 从这里 - https://github.com/kishikawakatsumi/SpreadsheetView
而不是添加标签,我的代码更改为:
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")
}
}
然而,在跑步的时候,我得到了一个SIGABRT,我是否正确地采用了这种方式。我看到很多关于添加像这样的控制器的文章 - Adding View controllers as subview in Swift - 但是请你帮我解决上述情况。
以下是我的截图:
答案 0 :(得分:1)
试试这个:
{{1}}
答案 1 :(得分:1)
您不需要SampleView
课程。我认为你因为@IBOutlet weak var spreadsheetView: SpreadsheetView!
而感到困惑。您不必遵循此程序。您只需创建一个实例并相应地使用它。
如果您尝试将view
SpreadsheetView
设为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
}
}
,请按以下方式使用:
@IBOutlet