How do I create a custom view in a custom table view cell?

jh95

I am trying to create a custom checkbox inside of a custom table view cell.

I have a custom table view cell:

class CustomTableViewCell: UITableViewCell {
   weak var delegate: CellDelegateT?
   @IBOutlet var customCheckbox: VKCheckbox!
}

Here's the code to load my tableView:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TransactionsTableViewCell
    let customCheckbox = cell.customCheckbox!
    customCheckbox.line = .thin
    customCheckbox.bgColor = UIColor.white
    customCheckbox.color = UIColor.black
    customCheckbox.borderColor = UIColor.black
    customCheckbox.borderWidth = 2
    customCheckbox.cornerRadius = customCheckbox.frame.height / 2
}

Where do I need to place the code that constructs the checkbox? Is the cellForRowAt not the correct place?

When I use the code outside of my table cell, it works fine. That's why I think I am placing the checkbox creation code in the wrong place.

fewlinesofcode

The answer to you question is "Yes, you should put checkbox settings to another place. Particularly I would suggest you to move it to CustomTableViewCell class"

There is an opinion, that good Object-oriented software design is based on SOLID principles.

SOLID is an acronym. And S stands for Single responsibility principle.. Simply saying - one class should be responsible for one thing and this responsibility should be encapsulated by the class.

In your case ViewController takes responsibility for the Cells subview layouting and adjustments. Which breaks at least ~S~ in SOLID

P.S. To be honest I am a bit afraid to write this answer, because of often arguments about good software design.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related