删除字典中的键并在Swift 2中重新加载TableView
我正在创建一个在ViewController中具有UITableView的应用程序。将在TableView中显示的数据位于NSDictionary中 让我解释一下我的项目是如何进行的,然后问我的问题: 还有我的Swift代码:删除字典中的键并在Swift 2中重新加载TableView,swift,uitableview,swift2,nsdictionary,nsindexpath,Swift,Uitableview,Swift2,Nsdictionary,Nsindexpath,我正在创建一个在ViewController中具有UITableView的应用程序。将在TableView中显示的数据位于NSDictionary中 让我解释一下我的项目是如何进行的,然后问我的问题: 还有我的Swift代码: import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var tableView: UITabl
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var dict:[Int:String] = [:]
var count:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dict.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:TableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TableViewCell
cell.label.text = self.dict[indexPath.row]! as String
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let alert = UIAlertController(title: "Remove?", message: "Touch in Remove", preferredStyle: .Alert)
let remove = UIAlertAction(title: "Remove", style: UIAlertActionStyle.Destructive) { (UIAlertAction) -> Void in
self.dict.removeValueForKey(indexPath.row)
self.tableView.reloadData()
}
let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alert.addAction(cancel)
alert.addAction(remove)
self.presentViewController(alert, animated: true,completion: nil)
}
}
@IBAction func addAction(sender: AnyObject) {
dict[count] = "Row \(count)"
++count
self.tableView.reloadData()
}
}
当我点击Add按钮时,会以升序(var count)生成一个键,并生成一个value=“Row(1)”,这非常有效
下一步是拆卸。添加了一个滑动以删除该行,效果非常好
但是,当我通过tableView.reload()为表充电时,会显示一个错误
信息是:
致命错误:在展开可选值时意外发现nil
我找到了发生错误的地方,但我无法修复
该错误是由于删除NSDiciary中的密钥而导致的。当我重新填充表时,indexPath.row接受数据dict[indexPath.row],但该键不存在
示例:如果在重新加载表时删除带有键2的行,则在字典中拾取键2数据时将发生致命错误
我试图验证字典中是否有这样一个键,但表中显示了一个空行。我不需要显示任何行
我的问题:当我们在TableView中使用键和字典时,如何返回或不显示不存在的行?这里是一个元组数组,以防您真的想使用该Int值。如果你不需要它,那就把它做成一个数组,就像其他人说的那样
@IBOutlet var tableView: UITableView!
var tuple = [(Int,String)]()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tuple.count
}
cell.label.text = tuple[indexPath.row].1
return cell
}
self.dict2.removeAtIndex(indexPath.row)
@IBAction func addAction(sender: AnyObject) {
tuple += [(count,"Row \(count)")]
++count
self.tableView.reloadData()
}
你必须使用
字典吗<代码>数组
和表格视图
就像花生酱和果冻一样,可以的话,我同意使用数组
的建议。如果没有,您可以尝试1)在返回之前隐藏将为空的单元格(这样可以避免空行),或者2)重建字典以避免空行(有点粗糙,但如果它不包含太多的项目,可能是可行的)。@mcclux,隐藏单元格不起作用,因为他的numberOfRowsInSection
基于dict.count
。如果他有3个项目并且删除了第二个项目,那么第三个项目将不会显示,因为indexPath.row
只会增加到1。从技术上讲,他可以使用他的count
变量,这是可行的,但他首先不应该使用字典。我的字典也处于信息时代,在项目结束前可能会涉及更多数据。我可以继续使用字典,或者应该找到另一种方法来组织我的数据?你可以使用元组来关联,比如说你有一个年龄、地址、电话号码、描述等的人。元组将非常适合这样做<代码>变量myTuples=[(年龄:Int,姓名:String,地址:String,强度:Double)]()myTuples+=[(10,“Dan”,“123 fake Street”,3.2)]myTuples[0]。年龄myTuples[0]。强度