Reputation: 31
So I can't seem to get this error away..
My code is:
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tblTasks: UITableView!
//UITableViewDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return taskMgr.tasks.count
}
//UITableViewDataSource
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "test")
cell.textLabel!.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel!.text = taskMgr.tasks[indexPath.row].desc
return cell
}
}
I used both of the required functions for UITableViewDataSource
so I'm not sure why I keep getting this error.
Upvotes: 3
Views: 3124
Reputation: 9778
You need to implement two required methods: tableView:numberOfRowsInSection and tableView:cellForRowAtIndexPath.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayDataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cell"
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell
// information here
return cell
}
If you have a custom cell, change only de line:
var cell:MyCustomCell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as MyCustomCell
Hope It helps!
Upvotes: 0
Reputation: 60414
Neither of each method's respective tableView
parameters should be optionals
Upvotes: 1
Reputation: 2668
Try changing the method signatures as mentioned below. Notice ! is removed in the input params.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
Upvotes: 3