如何在 swift 4.0 中将数据附加到标签的末尾
How to append data to the end of a label in swift 4.0
我有一个 table 视图,其中包含带有价格的杂货选项,这些选项存储在字典中。
一旦用户从 table 视图中选择一行,项目名称和价格就会添加到字典中并打印在控制台中。
问题是我也在尝试在标签中显示项目名称和价格供用户查看,但我似乎无法让它工作,因为每次用户单击新行时我都会迭代项目中的数据标签覆盖而不是附加到新行。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var items : Dictionary<Int, String> = [:]
@IBOutlet weak var groceryTable: UITableView!{
didSet {
groceryTable.dataSource = self
}
}
var groceryData:Dictionary<String, String> = ["Apple":"1", "Kiwi":"2", "Mango":"4", "Broccoli":"3","Milk":"4", "Eggs":"3", "Bread":"6"]
@IBOutlet weak var label: UILabel!
//selection of row handler
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Item added!")
let currentCell = tableView.cellForRow(at: indexPath) as! UITableViewCell
let val = currentCell.textLabel!.text
items.updateValue(val!, forKey: indexPath.row)
print(items)
for data in items{
let value = data.value
let line = "\n"
var a = ""
a.append(value + line)
self.label.text=a
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print("Item removed!")
items.removeValue(forKey: indexPath.row)
print(items)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groceryData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let key = Array(self.groceryData.keys)[indexPath.row]
let value = Array(self.groceryData.values)[indexPath.row]
cell.textLabel?.text = key + ", $"+value
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
实际结果,每次点击一行都会覆盖标签:
鸡蛋,3 美元
预计:
芒果,4 美元
面包,6 美元
鸡蛋,3 美元
你需要
self.label.text = self.label.text! + items.map { [=10=].value }.joined(separator:"\n")
终于找到答案了!
for data in items{
let value = data.value
label.text = label.text! + value + "\n"
items.removeValue(forKey: data.key)
}
我有一个 table 视图,其中包含带有价格的杂货选项,这些选项存储在字典中。
一旦用户从 table 视图中选择一行,项目名称和价格就会添加到字典中并打印在控制台中。
问题是我也在尝试在标签中显示项目名称和价格供用户查看,但我似乎无法让它工作,因为每次用户单击新行时我都会迭代项目中的数据标签覆盖而不是附加到新行。
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var items : Dictionary<Int, String> = [:]
@IBOutlet weak var groceryTable: UITableView!{
didSet {
groceryTable.dataSource = self
}
}
var groceryData:Dictionary<String, String> = ["Apple":"1", "Kiwi":"2", "Mango":"4", "Broccoli":"3","Milk":"4", "Eggs":"3", "Bread":"6"]
@IBOutlet weak var label: UILabel!
//selection of row handler
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Item added!")
let currentCell = tableView.cellForRow(at: indexPath) as! UITableViewCell
let val = currentCell.textLabel!.text
items.updateValue(val!, forKey: indexPath.row)
print(items)
for data in items{
let value = data.value
let line = "\n"
var a = ""
a.append(value + line)
self.label.text=a
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print("Item removed!")
items.removeValue(forKey: indexPath.row)
print(items)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groceryData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let key = Array(self.groceryData.keys)[indexPath.row]
let value = Array(self.groceryData.values)[indexPath.row]
cell.textLabel?.text = key + ", $"+value
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
实际结果,每次点击一行都会覆盖标签: 鸡蛋,3 美元
预计: 芒果,4 美元 面包,6 美元 鸡蛋,3 美元
你需要
self.label.text = self.label.text! + items.map { [=10=].value }.joined(separator:"\n")
终于找到答案了!
for data in items{
let value = data.value
label.text = label.text! + value + "\n"
items.removeValue(forKey: data.key)
}