lundi 19 décembre 2022

How can I efficiently dequeue custom table view cells in CellForRowAt function to avoid break Open-Closed Principle?

I am using a tableview having 8-10 custom tableview cells currently, In Future, we are planning to add more cells. Currently my CellForRowAt Function Looks like this

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
          let cell = tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath) as! FirstCell
            cell.configure(data: self.firstCellData)
            return cell
        } else if indexPath.row == 1 {
           let cell = tableView.dequeueReusableCell(withIdentifier: "secondCell", for: indexPath) as! SecondCell
            cell.configure(data: self.secondCellData)
            cell.productSpecificationsTableViewHeight.constant = cell.productSpecificationsTableView.contentSize.height
            return cell
        } else if indexPath.row == 2 {
        }.... 


and so on.. upto 10 Cells. is there any Software Engineering principle to do this in an efficient way without modifying view controller code.

I am thinking of creating a model for each Cell and pass indexPath.row and Custom Table View Cell in that class but still I have to use if-else blocks in that model too. Is there any other efficient way to do it without continuous use of if-elseif blocks and breaking OCP.

Aucun commentaire:

Enregistrer un commentaire