将项目水平附加到 tableView 单元格中,但不要让它们填满所有 space

Append item horizontally into a tableView cell but make them not fill all the space

我有一个想要实现的特定设计,它对我来说似乎并不复杂,但我正在努力实现它。

首先设计是这样的:

所以,我有一个 table 项目视图,其中有一张图片,然后是一个标题,然后是 collection 三个项目,它们将是 ImageViews。

所有这些都在一个单元格和一个 stackView 中。

我尝试制作一个水平堆栈视图并设法正确附加了我的项目,但它在设计方面出现了严重错误,因为我的图像视图一直水平拉伸。我想这不可能不拉伸这些项目。

我还尝试将 collection 视图添加到 table 视图中,但我发现对于这个基本的东西来说这非常复杂(我猜应该是这样)。然后,我来了

这是我的代码以及我遇到的问题:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = debateList.dequeueReusableCell(withIdentifier: "itemsBox", for: indexPath) as! ItemBox
        
        // Make a variable with 3 participants
        let participants = tableView[indexPath.row].participants?.prefix(3)
        // iterate over them and adding them to anything that can be doable
        participants?.forEach { participant in
            let imageView = UIImageView()
            let imageUrl = URL(string: participant.imageURL)
            imageView.kf.setImage(with: imageUrl)
            // Here add item to something
        }
        
        return cell
    }

我卡住了,这么小的东西已经太长了。猜猜我对自己没有弄清楚该怎么做感到有点不高兴。

首先如果它们是3张静态图片那么最好将它们放在设计中并将图片url分别分配给它们的网点

其次,对于您当前的工作,您需要添加宽度约束(关于高度,当它是水平堆栈时,它们将适合所有堆栈高度),例如

imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.widthAnchor.constraint(equalToConstant: 50).isActive = true

如果您最多有 5 个参与者,那么我建议您在水平堆栈视图中默认添加 2 个子视图:

  1. 宽度约束 >= 5 的空视图(称之为 spaceview
  2. "60 % no" UILabel 的固定宽度。

然后以编程方式insert image views with fixed width and aspect ratio 1:1 in horizontal stack view at index 0

这样,如果只有1-2个参与者,那么剩下的space会被spaceview占用,因为它的宽度会大于5

如果参与者超过 5 人,那么更好的选择是在水平堆栈视图中使用带有 uilabel 的 collectionview。