iosswiftxcodeuitableviewautolayout

Xcode 7.3 UITableViewController cell not displaying correctly


I try to display a simple cell with one label and an add item button. I can't get it to display correctly.

enter image description here

This is the result:

enter image description here

There is no add item button and no correct row. I have just two items in coredata.

This is my code:

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

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

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        
        let data = items[indexPath.row] as Item
        
        cell.textLabel?.text = data.body
        
        return cell
    }

What are the problems? How can I display add item button, correct row count, and customize height of the cell correctly?


Solution

  • First lets return the correct number of rows unto the table view.

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

    Next lets get it to output unto the prototype cells.

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    
        cell.textLabel?.text = cellContent[indexPath.row]
    
        return cell
    }
    

    Also lets call the unto the view controller the following so that it can run properly.

    ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

    One last thing, your prototype's cell has the same identifier as the one in your code. In my code I would make sure that the identifier in the prototype cell is "Cell" since I called it in UITableViewCell(...reuseIdentifier: "Cell"). View picture to see where to add the identifier in your storyboard