-1
import UIKit

class ToDoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var DateLabel: UILabel!
@IBOutlet weak var MonthLabel: UILabel!
@IBOutlet weak var DayLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var newButton: UIImageView!

static var ToDoEvents:[event] = []

override func viewDidLoad() {
    super.viewDidLoad()
    ToDoViewController.readFromFile()
    let nib = UINib(nibName: "CustomCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "customCell")
}

public static func readFromFile(){
    ToDoViewController.ToDoEvents.append(event(eventName: "Spanish Workbook", eventLength: 3601, complete: false))
    ToDoViewController.ToDoEvents.append(event(eventName: "History Test", eventLength: 37, complete: false))
    ToDoViewController.ToDoEvents.append(event(eventName: "Lit Essay", eventLength: 40, complete: false))
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return ToDoViewController.ToDoEvents.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCell
    let currentEvent = ToDoViewController.ToDoEvents[indexPath.row]
    let eventName = currentEvent.name!
    let eventLength = currentEvent.time!
    let completion = currentEvent.isComplete!
    print(eventName)
    print(eventLength)
    print(completion)

    cell.customInit(name: "\(eventName)", time: "\(eventLength)", completed: completion)
    return cell
}

}

Currently, I am trying to create an app with a ToDo list. I am using a UITableView to display the information, and I have a custom UITableViewCell called "CustomCell" that I am using to display the information in the table view. However, when I run this code, there is nothing showing up on the screen. I also added some print statements in the CellForRowAtIndexPath method, and those print statements are not getting executed. Then I tried to add

self.tableView.delegate = self
self.tableView.dataSource = self

however, when I try to run the app with this additional code, the app crashes giving the following error:

  Terminating app due to uncaught exception 

  'NSInternalInconsistencyException', reason: 
  'Could not load NIB in 
   bundle: 'NSBundle 
  </Users/sid/Library/Developer/CoreSimulator
  /Devices/812843C0-ABBA-43E9-A391- 
 0BED3CBB5030/data/Containers/Bundle
 /Application/F8DABFF7-80ED-49E1-8760-315C3C99AFB1/TimeIt.app> 
 (loaded)' with name 'CustomCell''

I have also watched multiple tutorials that outline how to go about adding a custom cell to your code, and none of these tutorials use the tableView.delegate = self or the tableView.dataSource = self code.

I am not sure what I am doing wrong. All help is appreciated

Thank You in advance!

James Ajjaja
  • 95
  • 1
  • 5

3 Answers3

0

I followed the below steps and i fixed the error:

  1. Open the XIB file causing problems
  2. Click on file’s owner icon on the left bar (top one, looks like a yellow outlined box)
  3. If you don’t see the right-hand sidebar, click on the third icon above “view” in your toolbar.
  4. This will show the right-hand sidebar In the right-hand sidebar, click on the third tab–the one that looks a bit like a newspaper Under “Custom Class” at the top, make sure Class is the name of the ViewController that should correspond to this view.
  5. If not, enter it In the right-hand sidebar, click on the last tab–the one that looks like a circle with an arrow in it, You should see “outlets” with “view” under it.
  6. Drag the circle next to it over to the “view” icon on the left bar (bottom one, looks like a white square with a thick gray outline.

Save the xib and re-run

0

Adding these two lines is the correct way to go:

self.tableView.delegate = self self.tableView.dataSource = self

Your crash log points you in the right direction. You don't have a xib file that is called "CustomCell".

If you aren't using a separate xib file for the UITableViewCell, then you need to set the proper class and identifier for your cell in the storyboard. If you set it in the storyboard then you don't need to register a xib for the table view.

Register nib's only if you are using separate files for you table view cells.

0

Seems like the register method are not find the right Nib. Maybe the name or identifier are wrong. You could use something like that:

func registerCell(){
    tableView.register(UINib(nibName: String(describing: CustomCell.self), bundle: nil), forCellReuseIdentifier: "customCell")
 }
ios_dev
  • 359
  • 2
  • 13