简体   繁体   English

Swift——带有按钮的自定义tableview单元格

[英]Swift -- Custom tableview cell with buttons

In my app I would like to have custom cells in my table view that displays a row of buttons that may display all buttons or fewer depending on data.在我的应用程序中,我希望在我的表格视图中有自定义单元格,该单元格显示一行按钮,这些按钮可能会根据数据显示所有或更少的按钮。 The cell looks like this:单元格看起来像这样:

在此处输入图片说明

Some of the buttons may not apply to every cell so some cells may look something like this.某些按钮可能不适用于每个单元格,因此某些单元格可能看起来像这样。

在此处输入图片说明

What are some ideas on how to structure this so only the buttons that are required show?关于如何构建它以便只显示所需按钮的一些想法是什么? If I hide a button the space remains.如果我隐藏一个按钮,空间仍然存在。 I would like all the buttons to start at the left and only show the ones that apply with no space in between.我希望所有的按钮都从左边开始,只显示那些中间没有空格的按钮。

Sounds like a job for a UIStackView .听起来像是UIStackView的工作。 They are wonderful for managing sets of evenly spaced objects like buttons.它们非常适合管理一组均匀间隔的对象,如按钮。

Put a stack view in your table view cell, and then have your tableView(_:cellForRowAt:) method remove any leftover buttons from the last time the cell was used and add the ones you need.在您的表格视图单元格中放置一个堆栈视图,然后让您的tableView(_:cellForRowAt:)方法从上次使用单元格时删除所有剩余的按钮并添加您需要的按钮。 You could even get fancy and manage an array of recycled buttons in order to avoid creating/destroying buttons each time a cell comes into view.您甚至可以花哨并管理一系列回收的按钮,以避免每次单元格进入视图时创建/销毁按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM