0

I have two tableviews with two different custom tableview cells.. and am doing this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == self.tableview {
        let cell = tableview.dequeueReusableCell(withIdentifier: "matchescell") as! MatchesTableViewCell
        // ......
        return cell
    } else {
        let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell
        // ......
        return cell
    }
}

and of course i have registered them both from storyboard...

enter image description here

enter image description here

enter image description here

enter image description here

but i keep getting this:

enter image description here

on this line:

let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell

tried to register them like this:

   tableview.register(MatchesTableViewCell.self, forCellReuseIdentifier: "matchescell")

   filterdrop.register(FilterTableViewCell.self, forCellReuseIdentifier: "filtercell")

but still getting the same error!

What am doing wrong?!

mrs.tat
  • 597
  • 2
  • 6
  • 17

7 Answers7

2

The problem is that you typed "tableview" instead of "tableView" in your else part:

replace :

let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell

with :

let cell = tableView.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell

It is crashing because your "tableview" doesn't have this cell registered.

marc
  • 914
  • 6
  • 18
0

Add the "filtercell" identifier in the storyboard or xib custom cell (FilterTableViewCell).

Kerberos
  • 4,036
  • 3
  • 36
  • 55
0

Possible cause:

  • You haven't registered the class or the nib of the cell to the proper tableview
  • You haven't set the identifier in the cellview in you nib/storyboard
  • You have inverted the tableview and their cell type
MarkWarriors
  • 544
  • 2
  • 9
  • 17
  • Please add the cell registration in your post. Then try a run and debug which of the 2 type of cell is nil before you return it – MarkWarriors Jul 18 '18 at 10:09
  • just did .. and the nil is the filtercell – mrs.tat Jul 18 '18 at 10:13
  • Ok, so, try to understand if, removing the delegate from the table filterdrop table, the other one work correctly. Let's see if the problem is only that table or we are missing somethign – MarkWarriors Jul 18 '18 at 10:23
  • yes exactly, removed delegates from filter tableview .. the other tableview works fine without errors – mrs.tat Jul 18 '18 at 10:26
  • You created a xib for the cell views or you create the cell inside their table? Can you provide the xib or the storyboard vc? – MarkWarriors Jul 18 '18 at 10:40
  • I have created the cell inside their tableview ... i found the answer .. it was a typo x_x wrote tableview other than tableView and everything is solved!! Thank you so much for your help! – mrs.tat Jul 18 '18 at 10:50
0

This because you either didn't register the cell

tableView.register(CustomCellClass.self, forCellReuseIdentifier: "CellID")

//

 tableView.register(UINib(nibName: "CustomCellClass", bundle: nil), forCellReuseIdentifier: "CellID")

or you swapped the dequeuing line for the tables

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

Have a look into your Tableview controller in the Storyboard. You should have another indentier than called in:

let cell = tableview.dequeueReusableCell(withIdentifier: "filtercell") as! FilterTableViewCell
mag_zbc
  • 6,801
  • 14
  • 40
  • 62
Fabian Gr
  • 215
  • 1
  • 12
0

BTW, you should always use this newer function

dequeueReusableCell(withIdentifier:for:)

instead of

dequeueReusableCell(withIdentifier:)

See this post for details.

Tom E
  • 1,530
  • 9
  • 17
  • Mmmh, you need to call the new function if you have different kind of size for the tableviewcell I think – MarkWarriors Jul 18 '18 at 10:03
  • @MarkWarriors: Nope, not really. Cell sizing is done either dynamically (`UITableViewAutomaticDimension`) or via the delegate method `tableView(_:heightForRowAt:)`. This is independent of how you dequeue the cell(s). – Tom E Jul 18 '18 at 11:11
  • yep, I know, but if all your cell have the same height and width, the "new function" is pretty useless, because is useless for the application search the size of the cell at that index.. because is the same size of the others – MarkWarriors Jul 18 '18 at 11:53
-1

You can easily solve this problem just to check what tableview you use at that time. you can try this code,

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) {

if (tableview == (yourtableview 1)) {

let cell = yourtableview 1.dequeueReusableCell(withIdentifier: "yourtableview Cell 1") as? yourtableview Cell 1

// write your code for tableview 1 return cell }

else { let cell = yourtableview 2.dequeueReusableCell(withIdentifier: "yourtableview Cell 2") as? yourtableview Cell 2

// write your code for tableview 2 return cell }

}

Romio Bera
  • 1
  • 1
  • 2