4
votes

I want to create three cells in a tableview controller and I added three prototype cells in storyboard it doesn't crash but it view an empty tableview.

 super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    let nib1 = UINib(nibName: "one", bundle: nil)
    tableView.registerNib(nib1, forCellReuseIdentifier: "one")
    let nib2 = UINib(nibName: "two", bundle: nil)
    tableView.registerNib(nib2, forCellReuseIdentifier: "two")
    let nib3 = UINib(nibName: "three", bundle: nil)

    tableView.registerNib(nib3, forCellReuseIdentifier: "three")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 3
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 1
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

// let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) var cell:UITableViewCell!

    switch (indexPath.row){
    case 0 :
        cell = tableView.dequeueReusableCellWithIdentifier("one") as! one
        cell.textLabel?.text = "book1"
        break;
    case 1:
        cell = tableView.dequeueReusableCellWithIdentifier("two") as! two
          cell.textLabel?.text = "book2"
        break;
    case 2 :
        cell = tableView.dequeueReusableCellWithIdentifier("three") as! three
          cell.textLabel?.text = "book3"

    default:
        break;

    }

    // Configure the cell...

    return cell
}
1
When you ask for help, please make sure to ask a clear question.ndmeiri
sorry what is not clearuser3662992
What is your question? You have only stated your problem.ndmeiri
I did everything right but none of the three cells are visible in the simulator without crash or errorsuser3662992
an empty tableview viewed only without datauser3662992

1 Answers

2
votes

Since you'll always want exactly 3 cells to show in your table view, you should use the "Static Cells" style of UITableView. You can set this in the Storyboard if you select the appropriate UITableView and look in the Attributes Inspector.

Then, drag out the correct number of cells and configure them in the Storyboard. Connect each cell to an IBOutlet if you need to manipulate them or their contents programmatically.

Note that, since static table views are only allowed in a UITableViewController, you may need to change the class that contains the code you included in your question.