Custom table view cell in Swift, without storyboard

I am new to learning iOS development and Swift and trying to figure out how to create a custom UITableViewCell without using Storyboards / Interface Builder. I would like to be able to execute everything in Swift code. So far, I have really been able to find those that use the Builder interface.

What I would like to do is create a reusable cell that can be created in any view of the table. From what I understand, I should be able to create a custom cell with subviews whose data can be set using the data in the table. Right?

Now I have a UINavigationController with a built-in, subclassed UITableViewController . Following some other tutorials, I also learned how to create a Struct and prepare test data for a table view cell. But about this, as far as I managed 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 that Swift is still very new, and probably why I cannot find too many resources for this topic, so if anyone has any pointers, I would be very grateful for your help!

+5
source share
1 answer

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

 //Cell.Swift import Foundation import UIKit class Cell : UITableViewCell{ //Do whatever you want as exta customization } 

ViewDidLoad controller

 override func viewDidLoad() { super.viewDidLoad() // register your class with cell identifier self.tableView.registerClass(Cell.self as AnyClass, forCellReuseIdentifier: "Cell") } //cellForRowAtIndexPath 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! } 
+9
source

Source: https://habr.com/ru/post/1202258/


All Articles