[英]Deleting table cell populated with Dictionary (Swift)
I'm trying to add functionality so the user can delete a table cell. 我正在尝试添加功能,以便用户可以删除表格单元格。 However, this table cell is populated using a dictionary so I'm getting an error message: "cannot invoke 'removeAtIndex' with an argument list of type {index: Int) 但是,此表单元格是使用字典填充的,因此我收到一条错误消息:“无法使用类型为{index:Int)的参数列表调用'removeAtIndex'
Here's my code: 这是我的代码:
var titles = [Int: String]()
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
titles.removeAtIndex(index: indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
You need to call removeValueForKey
method, because you use Int
as keys. 您需要调用removeValueForKey
方法,因为您将Int
用作键。 The 的
An you need to add beginUpdates
and endUpdates
method calls as well to perform the animations. 您还需要添加beginUpdates
和endUpdates
方法调用来执行动画。
See the code below: 请参见下面的代码:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
table.beginUpdates()
titles.removeValueForKey(index: indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
table.endUpdates()
}
}
Note: in such a case, consider using Array instead of Dictionary? 注意:在这种情况下,考虑使用Array而不是Dictionary? You don't actually need dictionary since it will be indexed from row 0 to row count-1, right? 您实际上并不需要字典,因为它将从第0行索引到第count-1行,对吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.