maffeyew maffeyew - 11 months ago 50
iOS Question

Error when creating tableview with multiple custom cells

I have encountered an error in

when attempting to create a
made up of custom
dependent upon a set of conditions.

Here is my code:

var tableData: [String] = []

@IBOutlet var tableView: UITableView!

override func viewDidLoad() {

tableView.delegate = self
tableView.dataSource = self

// number of rows in table view
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count

// create a cell for each table view row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let phonenocell:MyCustomCell = self.tableView.dequeueReusableCellWithIdentifier("phonecell", forIndexPath: indexPath) as! MyCustomCell

let pincell:SocialCell = self.tableView.dequeueReusableCellWithIdentifier("socialcell", forIndexPath: indexPath) as! SocialCell

let fbcell:FacebookCell = self.tableView.dequeueReusableCellWithIdentifier("facebookcell", forIndexPath: indexPath) as! FacebookCell

let snapcell:SnapchatCell = self.tableView.dequeueReusableCellWithIdentifier("snapchatcell", forIndexPath: indexPath) as! SnapchatCell

let twitcell:TwitterCell = self.tableView.dequeueReusableCellWithIdentifier("twittercell", forIndexPath: indexPath) as! TwitterCell

let instacell:InstagramCell = self.tableView.dequeueReusableCellWithIdentifier("instagramcell", forIndexPath: indexPath) as! InstagramCell

if tableData.contains("Number") {
return phonenocell

if tableData.contains("Social") {
return pincell

if tableData.contains("Facebook") {
return fbcell

if tableData.contains("Snapchat") {
return snapcell

if tableData.contains("Twitter") {
return twitcell

if tableData.contains("Instagram") {
return instacell


When attempting to build and run I get a build failed with the following fault:

"Missing Return in a function expected to return 'UITableViewCell'

I have been over and over my code but I honestly cannot see where I am going wrong...

Any help would be greatly appreciated!

Answer Source

You need to return cell for sure. You already do in conditions, but in case none of your condition statements would success, your return call wouldn't be fired.

Appending, for example:

return phonenocell

to the end of the function, should be quick fix for your code. It ensures, that the function will return a cell (that is mandatory).

My data source is the array tableData. This is constructed on the previous view as: @IBAction func switch1Toggled(sender: UISwitch) { if mySwitch1.on { fbTextBox.text = "Selected" dataArray.append("Facebook")

And this may be the main issue:

Assuming, that you choose 'facebook' and that you reload your tableView, every row will pass the first condition as it IS contained. You should put this in your method:

//assuming your data source contains multiple members, and your numberOfRowsInSections... method return tableData.count, you need to get each item for each row:

let currentTag = tableData[indexPath.row]
if (currentTag == "something") { //e.g. Facebook
    let somethingcell:MySomethingCell =  ... 
    self.tableView.dequeueReusableCellWithIdentifier("somethingcell", forIndexPath: indexPath) as! MySomethingCell
    return somethingcell
} else if {

return emptycell //this line is just for the case, when no of your conditions will pass and you don't catch all the situations...