如何查找重复项并在 table 视图中显示计数
How to find duplicate and show the count in table view
我有一个类似数组的列表,我需要在 table 视图中显示
let data = ["a", "a", "b", "c"]
在我的 table 视图中,我需要在每一行中显示所有字符串。我 done.But 我需要找到重复项并需要像说的那样更新计数。在我的数组中,我有 a
作为 2 counts
。
所以在我的 table 视图中,我需要显示
a(2)
b
c
任何帮助将不胜感激。谢谢
class TableVC: UIViewController {
//MARK: - Outlet & Properties
let data = ["a", "a", "b", "c"]
var unique = [String]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
unique = Array(Set(data)) // Here you will get Unique Elements of Array
}
}
//MARK: - UITableView datasource
extension TableVC : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return unique.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tableCell
cell.label.text = unique[indexPath.row] + " " + String(data.filter{[=10=] == unique[indexPath.row]}.count)
print(data.filter{[=10=] == unique[indexPath.row]}.count) // Here you will get COUNT of an Element.
return cell
}
}
我有一个类似数组的列表,我需要在 table 视图中显示
let data = ["a", "a", "b", "c"]
在我的 table 视图中,我需要在每一行中显示所有字符串。我 done.But 我需要找到重复项并需要像说的那样更新计数。在我的数组中,我有 a
作为 2 counts
。
所以在我的 table 视图中,我需要显示
a(2)
b
c
任何帮助将不胜感激。谢谢
class TableVC: UIViewController {
//MARK: - Outlet & Properties
let data = ["a", "a", "b", "c"]
var unique = [String]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
unique = Array(Set(data)) // Here you will get Unique Elements of Array
}
}
//MARK: - UITableView datasource
extension TableVC : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return unique.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tableCell
cell.label.text = unique[indexPath.row] + " " + String(data.filter{[=10=] == unique[indexPath.row]}.count)
print(data.filter{[=10=] == unique[indexPath.row]}.count) // Here you will get COUNT of an Element.
return cell
}
}