Search code examples
iosuitableviewswift

Custom table view cell in Swift, without Storyboard


I'm new to learning iOS development and the Swift language, and have been trying to figure out how to create a custom UITableViewCell without the use of Storyboards/Interface Builder. I'd like to be able to accomplish everything in Swift code. So far, I've only really been able to find ones that make use of the Interface Builder.

What I would like to be able to do is create a re-usable cell that can be instantiated in any table view. From what I understand I should be able to create a custom cell with sub-views, whos data can be set by the table view's incoming data. Right?

Right now I have a UINavigationController with an embedded, sub-classed UITableViewController. Following some other tutorials I've also learned how to create a Struct and prepare test data for the table view cell. But that's about as far as I've been able to get.

// My Table View Controller
class InventoryListViewController: MainTableViewController {

    let viewTitle = "Inventory"
    var inventoryItems = [InventoryItem]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = viewTitle.uppercaseString

        self.inventoryItems = [
            InventoryItem(name: "White Bread", expiry: "2014-09-12"),
            InventoryItem(name: "Mushrooms", expiry: "2014-09-15"),
            InventoryItem(name: "Watermelon", expiry: nil),
            InventoryItem(name: "Leftover Thai", expiry: "2014-09-15"),
            InventoryItem(name: "Cheddar Cheese", expiry: "2014-09-12"),
            InventoryItem(name: "Chicken Breasts", expiry: "2014-09-10"),
            InventoryItem(name: "Paprika", expiry: nil),
            InventoryItem(name: "Sour Cream", expiry: nil)
        ]

        // Right now this will fail without tableView:cellForRowAtIndexPath
        self.tableView.reloadData()
    }

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

}

// Sub-classed UITableViewController
class MainTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.separatorColor = UIColor.clearColor()
        self.tableView.contentInset = UIEdgeInsetsZero
    }

}

// Data struct for cells
struct InventoryItem {
    let name: String
    let expiry: String?
}

I understand Swift is still very new and that's probably why I can't find too many resources for this topic, so if anyone has any pointers I would be very appreciative to the help!


Solution

  • You can use below code for custom cells without storyboard. Just register your class and use it with tableViewController

    // Cell.Swift
    
    import UIKit
    
    class Cell: UITableViewCell {
    
        // Do whatever extra customization you need
    }
    

    Your controller's viewDidLoad

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // register your class with cell identifier
        self.tableView.registerClass(Cell.self as AnyClass, forCellReuseIdentifier: "Cell")
    }
    
      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            var cell: Cell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? Cell
            if cell == nil {
                cell = Cell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
            }
    
            // configure your cell custom code
             cell.textLabel?.text = @"Your Label Text"
            return cell!
        }