Custom UITableViewCell without Storyboard

I was looking for the way to make List function based on UITableView be directly editable text field. UITableView also offer editable by swipe function. But I wanted to make function to directly edit lists on UITableView. To make sure that, Custom UITableViewCell is needed, if without StoryBoard. Btw I prefer to generate content without using StoryBoard, it means my application completely depends on source code. 

Step 1: To make new class “CustomTableViewCell” by inheriting “UITableViewCell”.

class CustomTableViewCell: UITableViewCell{
    
    let TitleArea: UITextField = UITextField(frame: CGRect(x: 30, y: 5, width: 350, height: 25))
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String!)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        TitleArea.delegate = self
        contentView.addSubview(TitleArea)
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)!
    }    
}

and to implement this custom class without StoryBoard, most important thing is to define it on “TableView.registerClass” and “return value of UITableViewCell. Please refer below highlited line in sample source code.

class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var CusotmTableView: UITableView = UITableView()
	
    override func viewDidLoad() {
        super.viewDidLoad()
            
        // Get width and height of view.
        let displayWidth: CGFloat = self.view.frame.width
        let displayHeight: CGFloat = self.view.frame.height
        
        // Generate TableView
        CusotmTableView.frame = CGRect(x: 0, y: barHeight+30, width: displayWidth, height: displayHeight  )
        
        // [Important] To designate Custom UITableViewcells as UITableViewCell
        CusotmTableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "MyCell")
        
	CusotmTableView.dataSource = self
        CusotmTableView.delegate = self
        
        self.view.addSubview(CusotmTableView)
        

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

    }
	
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return ******
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "title"
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //CustomCell
        let cell:  CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! CustomTableCell
        cell.delegate = self
        
        return cell
    }
   
}

If you want to generate Custom UITableViewCell by source code without StoryBorder, Please make sure these two points. 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.