Started practice swift. In singleViewController I am trying to make a UITableView
. In storyboard I set the datasource and delegate. Here I am getting the error
Take out all the optionals for tableview and NSIndexpath for the latest Xcode 6.1.1 GM_Seed
Change the syntax of "UITableViewDataSource" protocol required methods as per new swift 3 documentation:
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
This has worked for me while compiling with swift 3 compiler
In general, a protocol has optional and required methods. For example, UISearchBarDelegate and UITableViewDelegate are the cases that you can declare to conform to a protocol without implementing any of their methods. But it does not work fine for UITableViewDataSource.
In the official documentation, Protocol "UITableViewDataSource" -> Symbols -> Configuring a Table View, the methods:
func tableView(UITableView, cellForRowAt: IndexPath)
and func tableView(UITableView, numberOfRowsInSection: Int)
shown with bold Required key word.
I had just faced the same problem in Swift.
You should realize all the functions for the UITableViewDataSource in the class, which means the following functions should be realized:
func numberOfSectionsInTableView(tableView: UITableView) -> Int{}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
I'm not sure whether missing the function for numberOfSectionsInTableView works for you or not. For me, I have to realize it in my class.
Copy the code below under the ViewController
class and specify the number of rows you want (in the 1st section) and define the content of each cell (in 2nd function)
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 //no. of rows in the section
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: <#T##UITableViewCellStyle#>, reuseIdentifier: <#T##String?#>)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}