展开单元格时,UITableViewCell中的标签移动/消失

时间:2018-12-19 06:37:58

标签: ios swift uitableview swift4 rx-swift

我正在尝试创建具有多个labelstextViewsimages的展开/折叠tableView。问题是当我展开一个单元格时,最上面的标签(图像中的黑色文本/蓝色背景)消失了,然后在单元格更新时又回来了。是否有适当的解决方案来解决此类问题?这和reloadRows有关吗?​​

enter image description here

// ViewController Class:

private func bindTableView() {
    guard let tableView = self.planServicesTableView,
        let viewModel = self.viewModel else {
        return
    }

    tableView.estimatedRowHeight = 130
    tableView.rowHeight = UITableView.automaticDimension

    let dataSource =  RxTableViewSectionedReloadDataSource<PlanServiceSection>(configureCell:
    {(dataSource: TableViewSectionedDataSource<PlanServiceSection>,
        tableView: UITableView,
        indexPath: IndexPath,
        item: PlanServiceSection.Item) -> UITableViewCell in

        let cell = tableView.dequeueReusableCell(withIdentifier: item.cellType.cellIdent, for: indexPath)

        if let planServiceCell = cell as? PlanServiceDescriptionTableViewCell {
            planServiceCell.setCollapsed(collapsed:(viewModel.cellIsExpanded(at: indexPath)) ? false : true)
            planServiceCell.configureCell(item: item)
            planServiceCell.upgradeTextView.sizeToFit()
            planServiceCell.featureDisclaimerTextView.sizeToFit()
        }

        if let disclaimerCell = cell as? PlanDisclaimerTableViewCell {
            disclaimerCell.setCollapsed(collapsed: (viewModel.cellIsExpanded(at: indexPath)) ? false : true)
            disclaimerCell.configureCell(item: item)
            disclaimerCell.disclaimerDescriptionTextView.sizeToFit()
        }

        return cell
    })

    viewModel.dataSource = dataSource
    tableView.tableFooterView = UIView()
    tableView.delegate = self

    viewModel.sections.bind(to: tableView.rx.items(dataSource: dataSource))
        .disposed(by: self.disposeBag)
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if let descriptionCell = tableView.cellForRow(at: indexPath) as? PlanServiceDescriptionTableViewCell {
        descriptionCell.setCollapsed(collapsed: shouldCollapseCell(indexPath: indexPath))
    }

    if let disclaimerCell = tableView.cellForRow(at: indexPath) as? PlanDisclaimerTableViewCell {
        disclaimerCell.setCollapsed(collapsed: shouldCollapseCell(indexPath: indexPath))
    }

    DispatchQueue.main.async {
        tableView.reloadRows(at: [indexPath], with: .automatic)
    }
}

private func shouldCollapseCell(indexPath: IndexPath) -> Bool {
    if let isExpanded = viewModel?.cellIsExpanded(at: indexPath),
        isExpanded {
        self.viewModel?.removeExpandedIndexPath(indexPath)
        return true
    }

    self.viewModel?.addExpandedIndexPath(indexPath)
    return false
}

// TableViewCell Class:

func setCollapsed(collapsed: Bool) {
    self.toggleArrowImage.image = (collapsed ? expandImage : collapseImage)
    self.stackView.isHidden = collapsed
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您需要执行以下操作来解决此问题

var cellHeights:[IndexPath:CGFloat] = [ : ]


override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeights[indexPath]{
return height
}
return UITableView.automaticDimension
}


override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeights[indexPath] = cell.frame.size.height
}

测试过后,让我知道!