我想知道如何为给定的nsobject中的每个项目添加价格并将其显示在表格中。非常感谢:)
class MenuItems:NSObject{
var sections:[String] = []
var items:[[String]] = []
var price: [[String]] = []
func addSection(section: String, item:[String]){
sections = sections + [section]
items = items + [item]
}
}
class AnnMenuItems: MenuItems {
override init() {
super.init()
addSection("Threading", item: ["Eye Brows","Upper Lip","Forehead","Chin","Sides","Face Waxing/Face Threading"])
addSection("Hair", item: ["Rebonding","Hair Dye","Head Massage","Streaking","Hair Cut","Straight Cut","Children Cut","Step Cut","Layer Cut"])
addSection("Waxing", item: ["Full Arms","3/4 Arms","Under Arms","Full Legs", "3/4 Legs","Half Legs"])
addSection("Hair Treatments", item: ["Hair Spa","Dandruff","Hair Fall Treatment","Galvanic Treatment", "Hair Wash","Colour/Henna/Oil"])
addSection("Facial", item: ["Clean Up Normal","Black Heads","Clean Up Special","Herbal Facial", "Fruit Facial"])`
}
}
答案 0 :(得分:2)
您可以创建一个字典,其中item-name可以是键,其价格可以是值。
根据您的设计,您还可以选择其他选项。例如:您可以创建名为Item
的结构/类,并添加name
,price
等属性。
不确定要显示的方式,但您可以使用UITableView显示商品列表及其价格。
答案 1 :(得分:1)
正如@ Tj3n建议的那样,您的数据结构应该具有更精细的粒度。我不确定你为什么要使用NSObject
子类 - 我更喜欢struct
s - 但在任何一种情况下你都可以这样做......
struct MenuItem {
var item: String
var price: String
}
struct MenuSection {
var sectionName: String
var items: [MenuItem]
}
struct MenuItems {
var sections:[MenuSection] = []
mutating func addSection(section: String, items:[MenuItem]){
let newSection = MenuSection(sectionName: section, items: items)
self.sections.append(newSection)
}
}
func defaultMenu() -> MenuItems {
var mi = MenuItems()
mi.addSection("Threading", items: [MenuItem(item: "Eye Brows", price: "10/6d"), MenuItem(item: "Upper Lip", price: "€1") /* etc. */])
mi.addSection("Hair", items: [MenuItem(item: "Rebonding", price: "$1"), MenuItem(item: "Hair Dye", price: "free") /* etc. */])
// etc...
return mi
}