If you want to have your own init method, you must: (a) make sure that you do not have a cell prototype (for example, if you do this, it will call the initializer itself); and (b) your cellForRowAtIndexPath properly handles when a reusable cell is not found, and therefore you must create your instance (with any parameters you want.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as MyTableViewCell? if cell == nil { cell = MyTableViewCell(param1: "foo", param2: "bar", reuseIdentifier: cellIdentifier)
Personally, however, I would not be inclined to follow this route. I would prefer to use cell prototypes and standard initialization procedures, but then, if necessary, just override the properties (or call some configuration function), for example:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as MyTableViewCell cell.property1 = "Foo" cell.property2 = "Bar" return cell }
Thus, I can enjoy the prototype of the cell, automatically create a class of cell objects, display IBOutlets and IBActions, etc., but perform any special configuration that I need here in cellForRowAtIndexPath .
source share