根据选择的动态高度MultipleSelectorRow,以显示所有选定的值

时间:2019-04-09 03:08:19

标签: ios swift eureka-forms

我正在使用Eureka构建表单,其中我们从列表中选择多个值,并且需要显示在表单上选择的所有值。我为此使用MultipleSelectorRow,但是没有选项可以根据内容动态增加单元格的大小。我们可以给定一个固定的高度,但是在这里我需要为单元分配一个动态的高度。请指导如何实现此目标?

我尝试给出一个固定的高度,它可以很好地工作,但是不能动态地确定单元的高度。我什至尝试实现UITableViewAutomaticDimension行高,但这也行不通。

<<< MultipleSelectorRow("aprovers") { row in
row.title = "Approvers"
row.options = requestedByArr
row.selectorTitle = "Select Approvers"
row.onPresent({ from, to in
// Decode the value in row title
to.selectableRowCellSetup = { cell, row in
// cell.height = ({return 60})
let size = row.cell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
row.cell.height = { size.height }
//row.cell.height = ({return UITableViewAutomaticDimension})
row.cell.detailTextLabel?.numberOfLines = 0
row.cell.contentView.setNeedsLayout()
row.cell.contentView.layoutIfNeeded()
row.reload()
self.tableView.reloadData()
if let value = row.selectableValue {
row.title = value
}
}
to.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: from, action: #selector(CategoryGroups.multipleSelectorDone(_:)))
})
row.onChange({ (row) in
//row.cell.height = ({return 100})
let size = row.cell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
row.cell.height = { size.height }
//row.cell.height = ({return UITableViewAutomaticDimension})
row.cell.detailTextLabel?.numberOfLines = 0
row.cell.contentView.setNeedsLayout()
row.cell.contentView.layoutIfNeeded()
row.reload()
self.tableView.reloadData()
})
}```

The expected results should be increased in cell's height as per the selected number of values from the multipleSelectorRow but the actually the height doesn't increase. If it increases, then UI gets distorted and data merge into the upper row.

1 个答案:

答案 0 :(得分:0)

我们需要实施

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100
tableView.delegate = self

使用方法

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

并将以下方法添加到MultipleSelectorRow

.cellSetup({ (cell, row) in
                cell.detailTextLabel?.numberOfLines = 0
            }).cellUpdate({ (cell, row) in

                cell.detailTextLabel!.translatesAutoresizingMaskIntoConstraints = false
                NSLayoutConstraint.activate([
                    cell.detailTextLabel!.leftAnchor.constraint(equalTo: (cell.textLabel?.rightAnchor)!, constant: 15),
                    cell.detailTextLabel!.rightAnchor.constraint(equalTo: cell.contentView.rightAnchor, constant: -15),
                    cell.detailTextLabel!.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -15),
                    cell.detailTextLabel!.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 15)
                    ])
                cell.updateConstraintsIfNeeded()
            })

不需要为高度实现任何其他方法。通过根据选择的值动态增加多个选择器行的高度,解决了我的问题。