无法在可扩展的Tableview iOS Swift中添加动态数据?

时间:2018-09-05 11:10:49

标签: ios swift uitableview expandablelistview

我是iOS开发的新手,我正在尝试使用来自服务器的动态数据来实现可扩展表。我正在使用https://github.com/amratab/ThreeLevelAccordian这个可扩展的表格视图。

在该库中,它们像下面的代码一样静态添加。

   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))

在cellforrowindexpath中,他们正像这样使用。

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()
    }

我想动态添加数据数组,而不是静态添加。到细胞。

示例:

cell.textLabel?.text = self.tableViewData[indexPath.row]

0 个答案:

没有答案