[英]set correct uibutton state after deleting cell from tableview in Swift
我是Swift的新手,對於這個特殊的問題,我很難下定決心。 因此,如果我無法立即傳達要點,請耐心等待。 ;-) 謝謝!
基本上,我在UITable中有一種待辦事項列表。 單元格具有一個UIButtons(帶有自定義類)作為“復選框”。 我將按鈕狀態存儲在Dictionary中,如下所示:
var buttonState = [listItem1 : true, listItem2 : false, ...]
字典也存儲到NSUserDefaults
。 一切正常。 我的問題是,當我使用UITableViewCellEditingStyle.Delete
函數刪除單元格並使用self.tableView.reloadData()
之后重新加載表時,按鈕狀態出現錯誤。 在上面的示例中,如果我刪除了listitem1,則listItem2將顯示為true。 實際上,存儲的值仍然為假。 我想這一定與indexPath有關。
注意:在self.tableView.reloadData()
之前,我確實將Dictionary更新為buttonState[listItem1] = nil
。 意思是我對於字典中的每個ListItem都有正確的值。 同樣,如果我完全重新加載視圖(即在模擬器中返回一個屏幕並從頭開始重新加載列表),則所有按鈕狀態都會正確顯示。
刪除表格單元格后如何在不重新加載整個屏幕的情況下立即獲得正確的按鈕狀態?
希望所有這些都以某種方式有意義! ;-)
// Seb
UITableView中的相關代碼:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellItem", forIndexPath: indexPath) as customCell
//Identify Button with Tag
cell.checkbox.tag = indexPath.row
// Set Label
switch indexPath.section{
case 0:
cell.textLabel?.text = "\(kleidung[indexPath.row])"
case 1:
cell.textLabel?.text = "\(hygiene[indexPath.row])"
default:
cell.textLabel?.text = nil
}
// Set Cell State
cell.onLoadSet()
return cell
}
// DELETE TABLE ROW
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if (editingStyle == UITableViewCellEditingStyle.Delete){
if (indexPath.section == 0) {
kleidung.removeAtIndex(indexPath.row)
let fixedPackItems = ",".join(kleidung)
packlisten![rowCount].setValue("\(fixedPackItems)", forKey: "kleidung")
} else if (indexPath.section == 1) {
hygiene.removeAtIndex(indexPath.row)
let fixedPackItems = ",".join(hygiene)
packlisten![rowCount].setValue("\(fixedPackItems)", forKey: "hygiene")
}
context.save(nil)
if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {
var itemString = storedToDoItems as? Dictionary<String, Bool>
state = itemString!
println(itemString)
}
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
identifier = "\(currentCell.textLabel!.text!)\(rowCount)"
state[identifier] = nil
println(state)
let checkedItems = state as Dictionary
NSUserDefaults.standardUserDefaults().setObject(checkedItems, forKey: "packItems")
NSUserDefaults.standardUserDefaults().synchronize()
self.packlisteTable.reloadData()
}
}
在customCell類中:
@IBAction func checkboxButton(sender: CheckBox) {
// Change Button State
if sender.isChecked == true {
sender.isChecked = false
}else{
sender.isChecked = true
}
//Retrieve stored state
if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {
var itemString = storedToDoItems as? Dictionary<String, Bool>
if itemString != nil {
state = itemString!
}
}
//Add new State to Stored
identifier = "\(textLabel!.text!)\(rowCount)"
state[identifier] = sender.isChecked
//Store state
let checkedItems = state as Dictionary
NSUserDefaults.standardUserDefaults().setObject(checkedItems, forKey: "packItems")
NSUserDefaults.standardUserDefaults().synchronize()
}
func onLoadSet(){
identifier = "\(textLabel!.text!)\(rowCount)"
//Set state from stored State
if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {
if storedToDoItems != nil {
var itemString = storedToDoItems as? Dictionary<String, Bool>
if itemString![identifier] != nil {
checkbox.isChecked = itemString![identifier] as Bool!
}
}
}
}
好,我找到了。 簡直太簡單了!
我只是添加了其他內容:
func onLoadSet(){
identifier = "\(textLabel!.text!)\(rowCount)"
//Set state from stored State
if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {
if storedToDoItems != nil {
var itemString = storedToDoItems as? Dictionary<String, Bool>
if itemString![identifier] != nil {
checkbox.isChecked = itemString![identifier] as Bool!
}
}
}
工作代碼:
func onLoadSet(){
identifier = "\(textLabel!.text!)\(rowCount)"
//Set state from stored State
if var storedToDoItems: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("packItems") {
if storedToDoItems != nil {
println(storedToDoItems)
var itemString = storedToDoItems as? Dictionary<String, Bool>
if itemString![identifier] != nil {
checkbox.isChecked = itemString![identifier] as Bool!
} else {
checkbox.isChecked = false
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.