1

I'm new to iOS development I'm trying to implement expandable table with dynamic data which comes from server. I'm using https://github.com/amratab/ThreeLevelAccordian this expandable table view.

In this library they added statically like below code.

   cells.append(TLAHeaderItem(value: "Bathroom" as AnyObject, imageURL: "bathroom_grey_32.png"))
        cells.append(TLACell(value: "Shower" as AnyObject))
        cells.append(TLASubItem(value: "Shower pores should be cleaned effectively by brushing." as AnyObject))
        cells.append(TLACell(value: "Tap" as AnyObject))
        cells.append(TLASubItem(value: "Taps must be washed with soap and all the salt removed." as AnyObject))
        cells.append(TLACell(value: "Toilet" as AnyObject, imageURL: "toilet_grey_32.png"))
        cells.append(TLASubItem(value: "Should be made stains and germs free." as AnyObject))

        cells.append(TLAHeaderItem(value: "Bedroom" as AnyObject, imageURL: "bedroom_grey_32.png"))
        cells.append(TLACell(value: "Bed" as AnyObject))
        cells.append(TLASubItem(value: "Remove all the dust." as AnyObject))
        cells.append(TLACell(value: "Dressing" as AnyObject))


        cells.append(TLAHeaderItem(value: "Kitchen" as AnyObject, imageURL: "kitchen_grey_32.png"))
        cells.append(TLACell(value: "Utensils" as AnyObject))
        cells.append(TLASubItem(value: "There are many type of utensils like tongs, rolling pin, pan, non stick pans. Wash them all." as AnyObject))
        cells.append(TLACell(value: "Sink" as AnyObject))
        cells.append(TLASubItem(value: "Clean the sink" as AnyObject))

in cellforrowindexpath they are using like this.

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = self.cells[(indexPath as NSIndexPath).row]
        let value = item.value as? String
        if let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) {
            cell.textLabel?.text = value
            let label = cell.textLabel!
            cell.imageView?.image = nil
            if let headerImage = item.imageURL, let image = UIImage(named: headerImage) {
                cell.imageView?.image = image
            }
            if let accessoryView = accessory(for: indexPath, and: .expand) {
                cell.accessoryView = accessoryView
            } else {
                cell.accessoryType = UITableViewCellAccessoryType.none
                cell.accessoryView = nil
            }

            if let _ = item as? TLAHeaderItem {
                if let headerFont = headerCellFont {
                    cell.textLabel?.font = headerFont
                }


                if let headerCellBackgroundColor = self.headerCellBackgrondColor {
                    cell.backgroundColor = headerCellBackgroundColor
                }
                if let headerCellTextColor = self.headerCellTextColor {
                    cell.textLabel?.textColor = headerCellTextColor
                }
            } else if (item as? TLASubItem != nil) {
                if isMultiline {
                    label.lineBreakMode = NSLineBreakMode.byWordWrapping
                    label.numberOfLines = 0
                    label.sizeToFit()
                }

                cell.accessoryView = nil
                cell.accessoryType = UITableViewCellAccessoryType.none

                if let subItemCellBackgrondColor = self.subItemCellBackgrondColor {
                    cell.backgroundColor = subItemCellBackgrondColor
                }
                if let subItemCellTextColor = self.subItemCellTextColor {
                    cell.textLabel?.textColor = subItemCellTextColor
                }
                if let subItemCellFont = self.subItemCellFont {
                    cell.textLabel?.font = subItemCellFont
                }
            } else {
                if let itemCellBackgrondColor = self.itemCellBackgrondColor {
                    cell.backgroundColor = itemCellBackgrondColor
                }
                if let itemCellTextColor = self.itemCellTextColor {
                    cell.textLabel?.textColor = itemCellTextColor
                }
                if let itemCellFont = self.itemCellFont {
                    cell.textLabel?.font = itemCellFont
                }
            }

            return cell
        }
        return UITableViewCell()
    }

instead of adding statically i want to add dynamically array of data. to the cell.

Example:

cell.textLabel?.text = self.tableViewData[indexPath.row]
Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
Code92
  • 79
  • 1
  • 10

0 Answers0