Load multiple uitableview cells under one xib in swift

I know that it is possible that we can load multiple uitableview cells under the same xib in Objective-C, but is this also possible in swift?

I tried using the same logic as in Objective-C

var cellAuditDetails:AuditDetailsTableViewCell! = tableView.dequeueReusableCellWithIdentifier("customTrialDetailsCell", forIndexPath: indexPath) as! AuditDetailsTableViewCell

            if indexPath.row == 0{
               if(cellAuditDetails == nil)
               {
                  cellAuditDetails = NSBundle.mainBundle().loadNibNamed("AuditDetailsTableViewCell", owner: self, options: nil)[0] as! AuditDetailsTableViewCell;
               }
            }
            else{
                  cellAuditDetails = NSBundle.mainBundle().loadNibNamed("AuditDetailsTableViewCell", owner: self, options: nil)[1] as! AuditDetailsTableViewCell;
}

enter image description here

But the following error turned out ***Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (customTrialDetailsCell) - nib must contain exactly one top level object which must be a UITableViewCell instance'

Now, if I use only one cell, then its fine. But how can I load multiple cells in the same xib? Because it annoys him to take another xib for every new cell.

+4
source share
2 answers

Perhaps if you distinguish between the representations in your XIB (tags, custom classes, etc.), you don’t even need to register nib - all you have to do is do the following:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cellIndex = 12345
    var cellIdentifier = "MyAwesomeCell"

    // dequeue cell or, if it doesn't exist yet, create a new one from the nib
    var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
    if !cell {
        var topLevelObjects = Bundle.main.loadNibNamed("MyNibWithLotsOfCustomCells", owner: self, options: nil)
        var index = (topLevelObjects as NSArray).indexOfObject(passingTest: {(_ obj: Any, _ idx: Int, _ stop: Bool) -> BOOL in
                // if you distinguish the views by class
                return type(of: obj) === NSClassFromString(cellIdentifier)

                // if you distinguish the views by tag
                return (obj as! UIView).tag == cellIndex
            })
        cell = topLevelObjects[index]
    }
}

, ,

dequeueReusableCell(withIdentifier:cellIdentifier)

dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
0

- Swift 3. @waris-shams. :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     var cell = tableView.dequeueReusableCell(withIdentifier: "customCell")

     if indexPath.row == 0{
        if(cell == nil)
        {
             let cellAuditDetails:AuditDetailsTableViewCell = Bundle.main.loadNibNamed("AuditDetailsTableViewCell", owner: self, options: nil)? [0] as! AuditDetailsTableViewCell
             cell = cellAuditDetails
         }
      }
      else{
           if(cell == nil)
           {
              let cellAuditDetails:AuditDetailsTableViewCell = Bundle.main.loadNibNamed("AuditDetailsTableViewCell", owner: self, options: nil)? [1] as! AuditDetailsTableViewCell
              cell = cellAuditDetails
           }
      }
}
0

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


All Articles