我正在尝试使用滑动删除功能来删除tableview单元格。除了只剩下1个单元格时,它工作正常。如果我删除最后一个单元格,我会得到标题错误消息。这里出了什么问题?这似乎是一个内部逻辑错误,但我在删除行之前从数组中删除了项,所以行不应该反映这一点吗?提前感谢!
以下是一些设置:
private var resultArray: [SavedResult] = []
private var decodedData = [SavedResult]()
@IBOutlet weak var resultsTableView: UITableView!
var resultLabel = "none"
override func viewDidLoad() {
super.viewDidLoad()
self.resultsTableView.delegate = self
resultsTableView.dataSource = self
resultsTableView.rowHeight = 80.0
loadCellName()
}
func loadCellName() {
resultArray = []
if let data = try? Data(contentsOf: filePath!) {
let decoder = PropertyListDecoder()
do {
decodedData = try decoder.decode([SavedResult].self, from: data)
} catch {
print("There was an error loading data: \(error)")
}
}
resultArray = decodedData
self.resultsTableView.reloadData()
}
下面是我所有tableview/数据源/swipe内容:
extension SavedViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if resultArray.count == 0 {
return 1
} else {
return resultArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = resultsTableView.dequeueReusableCell(withIdentifier: "ResultCell", for: indexPath) as! SwipeTableViewCell
cell.delegate = self
if resultArray.count == 0 {
cell.textLabel?.text = "No Saved Results Yet."
cell.textLabel?.attributedText = NSAttributedString(string: (cell.textLabel?.text)!, attributes: [NSAttributedString.Key.font : UIFont(name: "Caveat", size: 30.0)])
cell.backgroundColor = blueCell
cell.accessoryType = .none
cell.isUserInteractionEnabled = false
return cell
} else {
cell.textLabel?.text = resultArray[indexPath.row].name
cell.textLabel?.attributedText = NSAttributedString(string: (cell.textLabel?.text)!, attributes: [NSAttributedString.Key.font : UIFont(name: "Caveat", size: 30.0)])
switch indexPath.row {
case 0, 3, 6, 9, 12, 15, 18, 21, 24, 27:
cell.backgroundColor = blueCell
case 1, 4, 7, 10, 13, 16, 19, 22, 25:
cell.backgroundColor = yellowCell
case 2, 5, 8, 11, 14, 17, 20, 23:
cell.backgroundColor = purpleCell
default:
fatalError("cell background colour setting failed.")
}
return cell
}
}
}
extension SavedViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
resultLabel = resultArray[indexPath.row].resultMessage
resultsTableView.deselectRow(at: indexPath, animated: true)
self.performSegue(withIdentifier: "savedExamSeg", sender: self)
}
}
extension SavedViewController: SwipeTableViewCellDelegate {
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
self.resultArray.remove(at: indexPath.row)
let encoder = PropertyListEncoder()
do {
let data = try encoder.encode(self.resultArray)
try data.write(to: self.filePath!)
} catch {
print("There was an error saving data: \(error)")
}
}
deleteAction.image = UIImage(named: "deleteIcon")
return [deleteAction]
}
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
var options = SwipeTableOptions()
options.expansionStyle = .destructive
return options
}
1条答案
按热度按时间f8rj6qna1#
您必须通知表视图删除该行