這是我的sectionArray
var sectionArray = [Sections]()
這就是我如何計算部分中有多少行
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionArray[section].items.count
}
這是我通過滑動洗掉一行的代碼。
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
sectionArray[indexPath.section].remove(at: indexPath.row)
if sectionArray[indexPath.section].title.count == 0 {
sectionArray.remove(at: indexPath.row)
}
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
}
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
//self.isEditing = false
print("delete button tapped")
}
delete.backgroundColor = UIColor.red
return [delete]
}
從理論上講,這個洗掉的東西應該可以作業,但我收到錯誤沒有完全匹配的呼叫下標
sectionArray[indexPath.section].remove(at: indexPath.row)
uj5u.com熱心網友回復:
/// This line
sectionArray.remove(at: indexPath.row)
/// should be like this
sectionArray.remove(at: indexPath.section)
/// This line will remove the section from the table
tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)
您正在使用行來洗掉一個部分
編輯
用這個替換你提供給我們的所有代碼
/*
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
sectionArray[indexPath.section].remove(at: indexPath.row)
if sectionArray[indexPath.section].title.count == 0 {
sectionArray.remove(at: indexPath.row)
}
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
}
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
//self.isEditing = false
print("delete button tapped")
}
delete.backgroundColor = UIColor.red
return [delete]
}
*/
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
//self.isEditing = false
sectionArray[indexPath.section].remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
if sectionArray[indexPath.section].items.isEmpty {
sectionArray.remove(at: indexPath.section)
tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)
}
}
delete.backgroundColor = UIColor.red
return [delete]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/447187.html
上一篇:透明部分標題?
