Reputation: 11
I started writing my first apps using MVC, and i dont know with code version is better on this pattern.
In viewcontroller:
let quantity = quantityProducts[indexPath.row]
let nameProduct = productsArray[indexPath.row]
cell.configurateWithItem(quantity: quantity, name: nameProduct)
In cellClass:
class NeedProductsTableViewCell: UITableViewCell {
@IBOutlet weak var quantityLabel: UILabel!
@IBOutlet weak var productNameLabel: UILabel!
func configurateWithItem(quantity:String,name:String){
quantityLabel.text = quantity
quantityLabel.textColor = .systemGray
quantityLabel.font = quantityLabel.font.withSize(14)
productNameLabel.text = name
productNameLabel.font = UIFont.boldSystemFont(ofSize: 14)
}
or maybe standard verion (set cell properties in view controller)?
let cell = tableView.dequeueReusableCell(withIdentifier: "NeedProductsTableViewCell", for: indexPath) as! NeedProductsTableViewCell
cell.quantityLabel.text = quantityProducts[indexPath.row]
cell.quantityLabel.textColor = .systemGray
cell.quantityLabel.font = cell.quantityLabel.font.withSize(14)
cell.productNameLabel.text = productsArray[indexPath.row]
cell.productNameLabel.font = UIFont.boldSystemFont(ofSize: 14)
Upvotes: 0
Views: 82
Reputation: 16794
The answer might be opinion based but I can really see no arguments toward using the second "standard version" approach.
This goes beyond MVC but you should keep your public interface as closed as possible, only expose properties that make sense. So use private
in most cases.
So in your cell I suggest
@IBOutlet private var quantityLabel: UILabel!
@IBOutlet private var productNameLabel: UILabel!
You may persist weak
though it is not needed. You could also use optionals instead of force-unwrapped values quantityLabel: UILabel?
instead of quantityLabel: UILabel!
to improve stability.
So once you have internal parts private you need to check how to expose your interface. This still has multiple patterns; one is what you used, your cell does not save the object you inject but just modifies its visual representation. The other one would look like this:
class NeedProductsTableViewCell: UITableViewCell {
@IBOutlet private var quantityLabel: UILabel?
@IBOutlet private var productNameLabel: UILabel?
var item: (quantity: String, name: String)? {
didSet {
if let item = item {
quantityLabel?.text = item.quantity
quantityLabel?.textColor = .systemGray
quantityLabel?.font = quantityLabel.font.withSize(14)
productNameLabel?.text = item.name
productNameLabel?.font = UIFont.boldSystemFont(ofSize: 14)
}
}
}
In this case you would use in your data source:
cell.item = (quantityProducts[indexPath.row], productsArray[indexPath.row])
This also suggests that you should have only 1 array if possible. Either using a structure to encapsulate both values or to simply use tuples:
private var items: [(quantity: String, name: String)] = []
instead of
private var quantityProducts: [String] = []
private var productsArray: [String] = []
Upvotes: 3
Reputation: 1456
If you want to use the Single responsibility principle the best thing you can do is not to have the delegate
of the UITableView
to your UIViewController
and to have a new class called YourTableViewController: UITableViewController and assign that class to your tableView in the storyboard.
Now your UIViewController
handles the UIViewController
issues, UITableView
class handles the UITableView
issues, and everything that has to do with the UITableViewCell
is being handled by the CustomUITableViewCell
class.
So the best is to call the configurateWithItem()
function after you create your cell but from your UITableView
custom class.
By following this rule you keep your UIViewControllers
small and you don't create Massive View Controllers. Also UITableViewControllers
can be reused in different UIViewControllers
.
Upvotes: 0