Swift TableView pour iOS

J'ai fait reference à cette question et j'ai essayé de mettre en œuvre les différentes réponses. Je fais quelque chose de stupide, car cellForRow n'est pas appelé, et je reçois juste un tableau vide. Existe-t-il d'autres methods de source de données ou de délégué que je dois replace?

Merci

class ViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //tableView.delegate = self //tableView.dataSource = self tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell") //tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell") tableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { println("cellForRowAtIndexPath") println() //variable type is inferred var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell if !cell { cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL") } cell!.textLabel.text = "Text Label" cell!.detailTextLabel.text = "Detail Text Label" return cell } } 

MISE À JOUR – Ce qui suit semble fonctionner .. Merci pour l'aide !!

 class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //tableView.delegate = self //tableView.dataSource = self tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell") //tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell") tableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { return 10 } override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { println("cellForRowAtIndexPath") println() //variable type is inferred var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell if !cell { cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL") } cell!.textLabel.text = "Text Label" cell!.detailTextLabel.text = "Detail Text Label" return cell } } 

Voici le code de travail. Pas besoin d'save la cellule dans -viewDidLoad() . UITableView est également ajouté en tant que sous-vue dans Storyboard.

Si quelqu'un a ce genre d'exigence.

 import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() self.tableView.delegate = self self.tableView.dataSource = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as UITableViewCell? if (cell == nil) { cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL") } cell?.textLabel?.text = NSSsortingng(format: "%d", indexPath.row) as Ssortingng return cell! } } 

Vous devez vous connecter Delegate DataSource de TableView , les deux sont connectés à la vue de tableview puis seulement NumberOfRowsInSection et CellForRowAtIndexpath appelés.