David
David

Reputation: 1680

Swift | Why my UITableView is empty (customized cell)

My tableview app looks like this:

Empty tableview

I don't know why that label1 is not working.

Here's how I done this:

I dragged a UITableViewController and set it as initialViewController.

Main.storyboard

I set the UITableViewCell style as Custom

UITableViewCell style

Then I set that UITableViewController's Custom Class

UITableViewController class

and UITableViewCell's

UITableViewCell class

Then, I added some constraints:

UITableViewCell label's constraints

Here's TableViewController.swift:

import UIKit

class TableViewController: UITableViewController {

    let arr = ["abc", "def", "ghi", "jkl", "mno"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1 // This should more than one if you want to see the sections and rows in your tableview
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("item", forIndexPath: indexPath) as! TableViewCell
        cell.label1.text = arr[indexPath.row]
        return cell
    }
}

And here's TableViewCell.swift

import UIKit

class TableViewCell: UITableViewCell {
    @IBOutlet weak var label1: UILabel!
}

Anyone knows how to solve this? I really need to use custom cell!

P.S. I'm using Xcode 7 beta 5, Swift 2.0

Upvotes: 0

Views: 1927

Answers (1)

Jincy Sam
Jincy Sam

Reputation: 421

try this

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

Upvotes: 5

Related Questions