【发布时间】:2019-05-28 08:38:30
【问题描述】:
我有一个 CollectionView,其中包含...
Fruit(name: String, price: Int, imageUrl: String)
用户可以选择多个Fruits,然后进行“结帐”,这是一个UITableView,并选择了所有水果。
UITableViewCell 包含:amountLabel、fruitLabel、priceLabel。
我想在这个视图中显示每种水果的数量和总价格。
最好的方法是什么?
我现在看到的:
Banana - 1$
Orange - 3$
Banana - 1$
Apple - 2$
Apple - 2$
Banana - 1$
Orange - 3$
Orange - 3$
total: 16$
我想看的:
3x Banana - 3$
2x Apple - 4$
3x Orange - 9$
total: 16$
【问题讨论】:
-
你应该将你的 Fruit 包装在一个 Order 结构体中,并在添加相同水果时增加一个 int 计数器,而不是存储一个单独的水果。然后,您还可以在 Order 结构中拥有一个计算属性,该属性返回总金额。 Order 对象可以存储在以水果或水果名称为键的字典中
-
@JoakimDanielson 他想用这个数据源做到这一点。这是他的问题。
-
@RakeshaShastri 我将此作为评论发布,而不是作为完全不同的答案,因此我认为我可以提出替代路线供 OP 考虑。
标签: arrays swift uitableview