Reputation: 37
Using just theTable Views, I added a label to the first cell, I tried to drag it to the view controller to ad code, but it didn't work. (Still very new to programming)
This is all that's in the view controller now.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
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.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_tableView:UITableView, cellForRowAtIndexPath index, Path: NSIndexPath) -> UITableViewCell{
return
}
}
Also for this, I get "parameter requires an explicit type error
func tableView(_tableView:UITableView, cellForRowAtIndexPath index, Path: NSIndexPath) -> UITableViewCell {
return
}
Upvotes: 0
Views: 141
Reputation: 849
You have to create a class of type UITableViewCell
class sampleCell: UITableViewCell {
@IBOutlet weak var _myLabel: UILabel! // you have to drag from story board.
}
//Storybord
select the cell where you put the label.
click on the cell -> select identity inspecter -> custom class ->set the custom class name. // "sampleCell"
select the next tab attribute inspector -> identifier set the name as "sampleCell" // you can put any name
//ViewController
set outlet tableview.
@IBOutlet weak var _tableView: UITableView! // drag from viewcontroller
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = _tableView.dequeueReusableCellWithIdentifier("sampleCell", forIndexPath: indexPath) as! sampleCell
cell._myLabel.text = "sample"
return cell
}
Upvotes: 0
Reputation: 810
Provide a class to a view controller and you have to return a UITableViewCell. once class is provided select lable and drag it to the file.
Upvotes: 1
Reputation: 562
For the first part, be sure to assign the class of the view controller in the storyboards, otherwise, Xcode doesn't allow you to create @IBOutlets by dragging the label.
The second one, the function is expected to return a UITableViewCell, and you're not returning anything. Also, if you're using Swift 3, this is the new syntax. A quick fix for you to check would be the following:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
Upvotes: 0