Fast subclasses of multiple cells in a UITableViewCOntroller

I am trying to add several subclasses to a UITableView. The problem is that he continues to give me the following error:

Type UITableVieCell does not conform to protocol NilLiteralConvertible 

CellForRowAtIndexPath

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.section == 0 { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell cell.textLabel?.text = self.section1[indexPath.row] cell.accessoryType = .DisclosureIndicator return cell } else if indexPath.section == 1 { let cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell cell.cellLabel?.text = self.section2[indexPath.row] return cell } return nil } 
+7
source share
4 answers

You are not allowed to return nil for cellForRowAtIndexPath

You can use at the end:

 let cell:UITableViewCell! return cell 

instead

  return nil 

This should be (if you have only 2 partitions) will never be executed.

Edit:

You can also use instead of "} else if indexPath.section == 1 {" only} else {- to return the cell. I just found out what the problem is. Or use Switch / Case and return a blank cell by default.

-3
source

You cannot return zero. Instead, it returns an empty cell by default.

  var cell :UITableViewCell! switch (indexPath.row) { case 0: cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell cell.textLabel?.text = self.section1[indexPath.row] cell.accessoryType = .DisclosureIndicator break; case 1: cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell (cell as SwitchViewCell).cellLabel?.text = self.section2[indexPath.row] break; default: break; } return cell 

Make sure you register nib

  self.tableView.registerNib(UINib(nibName: "SwitchViewCell", bundle: nil), forCellReuseIdentifier: "SwitchViewCell") 
+8
source

tableView:cellForRowAtIndexPath: should return a UITableViewCell and cannot return nil . Therefore you need to remove return nil . But that will not be enough. Your if else should also be completed. This means that all possible section values ​​should be provided, or at least sent to the passage.

Your if else should look like this:

 if indexPath.section == 0 { /* ... */ } else if indexPath.section == 1 { /* ... */ } else { /* ... */ } 

Or like this:

 if indexPath.section == 0 { /* ... */ } else { /* ... */ } 

However, the following if else not complete:

 if indexPath.section == 0 { /* ... */ } else if indexPath.section == 1 { /* ... */ } 

In this case, tableView:cellForRowAtIndexPath: will not know what to return if any of these conditions are checked. Thus, if you try, Xcode (smart) will also display an error message:

There is no return in a function that should return a 'UITableViewCell'

Therefore, the following code should work:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.section == 0 { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell cell.textLabel?.text = self.section1[indexPath.row] cell.accessoryType = .DisclosureIndicator return cell } else { let cell = tableView.dequeueReusableCellWithIdentifier("SwitchViewCell", forIndexPath: indexPath) as SwitchViewCell cell.cellLabel?.text = self.section2[indexPath.row] return cell } } 

PS:

If you also use the switch inside your tableView:cellForRowAtIndexPath: method to set your cells, this answer to a similar question may help you.

+6
source

In my situation, I did the following:

 let cellOne = mTableView.dequeueReusableCell(withIdentifier: "CellOne") as! customTableViewCellOne let cellTwo = mTableView.dequeueReusableCell(withIdentifier: "CellTwo") as! customTableViewCellTwo if (..something...) { cellOne.mLabel = "hey" return cellOne } else if (..another condition..) { cellTwo.mButton.layer.cornerRadius = 5 return cellTwo } return UITableViewCell() 

I hope this helps

0
source

Source: https://habr.com/ru/post/976103/


All Articles