My doubt is about "Prototype Cells" and the method "dequeueReusableCellWithIdentifier" below:
func dequeueReusableCellWithIdentifier(_ identifier: String, forIndexPath indexPath: NSIndexPath) -> AnyObject
Let's say that I create a View Cotroller in StoryBoard and attach a Table View to it. In this first scenario I won't attach a Table View Cell to the Table View. Then I create the following class to control my View Controller:
import UIKit
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var myArray: String = [String]["One", "Two", "Three"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
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, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("testCell") as UITableViewCell!
// I NEED THIS HERE
if (cell == nil) {
cell = UITableViewCell()
}
cell.textLabel!.text = "Test"
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.myArray.count
}
}
Sorry for bad indentation.
In this situation I need the if statement
in func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
, to check if the method dequeueReusableCellWithIdentifier
returned nil
. If in the StoryBoard I attach a Table View Cell, which will create Protype Cells, to the Table View previously attached and set its identifier to "testCell", do I still need the if statement
above? I guess not because I will always have cells to be dequeued, but am I right? I am not considering here the situation where I have created the maximum amount of cells allowed.
Thanks.