菜鸟教程小白 发表于 2022-12-11 22:41:15

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


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

<p>我试过给定一个固定的高度,它工作得很好,但动态决定单元格的高度不起作用。我什至尝试实现 UITableViewAutomaticDimension 行高,但这也不起作用。</p>

<pre><code>&lt;&lt;&lt; MultipleSelectorRow(&#34;aprovers&#34;) { row in
row.title = &#34;Approvers&#34;
row.options = requestedByArr
row.selectorTitle = &#34;Select Approvers&#34;
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&#39;s height as per the selected number of values from the multipleSelectorRow but the actually the height doesn&#39;t increase. If it increases, then UI gets distorted and data merge into the upper row.
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我们需要实现</p>

<pre><code>tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100
tableView.delegate = self
</code></pre>

<p>用方法</p>

<pre><code>override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -&gt; CGFloat {
    return 100
}
</code></pre>

<p>并将以下方法添加到 MultipleSelectorRow</p>

<pre><code>.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()
            })
</code></pre>

<p>没有必要为高度实现任何其他方法。这通过根据所选值动态增加多个选择器行的高度解决了我的问题。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 动态高度 MultipleSelectorRow 根据选择显示所有选定的值,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55584542/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55584542/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 动态高度 MultipleSelectorRow 根据选择显示所有选定的值