![](/img/trans.png)
[英]When I delete cell from tableview cells that are not deleted are duplicated SWIFT
[英]TableView cells are repeated/duplicated
所以目前我正在为大学做一个 Swift 项目。 它有一个 TableView,它应该有 10 个单元格,因为我的数据数组有 10 个索引。 我能够设置应用程序,除了在 10 个条目之后,从第 1 个条目到最后一个条目重复整个数据集的事实之外,一切正常……这就是问题所在。 . (TableData 是保存单元格数据的数组)
override func numberOfSections(in tableView: UITableView) -> Int {
return tableData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)
let mapLocation = tableData[indexPath.row]
cell.textLabel?.text = mapLocation.name
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedLocation = tableData[indexPath.row]
performSegue(withIdentifier: "fromTableToMap", sender: selectedLocation)
}
你有
override func numberOfSections(in tableView: UITableView) -> Int {
return tableData.count
}
将其替换为
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
否则,您将 10 行重复 10 次(制作 10 个部分),结果为 100 行。
用这个。
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
您缺少UITableView
的基本概念。 一个 TableView 由一个或多个部分组成,每个部分由一个或多个单元格组成。
override func numberOfSections(in tableView: UITableView) -> Int {
return tableData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
这意味着您使用 10 个作为部分,每个部分有 10 行。 所以它返回 10 * 10 = 100 个单元格(您可以手动计数)
它应该是
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
现在您使用 1 作为部分,每个部分有 10 行。 所以它返回 1 * 10 = 10 个单元格(您可以手动计数)
只需从您的代码中删除 numberOfSections 方法,默认情况下它将为 tableView 占用一个部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.