2016-06-19 22 views
0

好吧,我已經看到了OBJç以前的問題,並嘗試過。我試圖建立的是一個類似iphone的通訊錄應用程序。我在插入表格中存在這個問題。之前我從來沒有插入行這個問題,我可能認爲這是因爲我的數據結構不好?「NSInternalInconsistencyException」,理由是:「試圖插入一行4到段0,但更新後,只有4節0行」

我嘗試的解決方案是tableView.beginUpdates和endUpdates但還是同樣的錯誤。這是我的聯繫人列表的代碼。

class ContactsListViewController: UITableViewController, AddContactViewControllerDelegate { 

    let cellID = "contactCellID" 
    var sectionNames = ["A","B","C","D"] 
    var name = [0: ["Amber", "Ajay", "Abhishek", "Anshul"], 1: ["Borat", "Bruno", "Billooo"], 2: ["Chinku","Champu","Champak"], 3: ["Daya", "Divya","Delhi Police"]] 

//  
// @IBOutlet weak var addBarOutlet: UIBarButtonItem! 
// @IBAction func addBarButton(sender: UIBarButtonItem) { 
//  performSegueWithIdentifier("AddItemSegueID", sender: addBarOutlet) 
// } 
// 

    //var nameSet = ["0": "Amber", "1": "Blah", "2": "Chris" ] 
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return name.count 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     let array = name[section] 
     return (array!.count) 
    } 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) 
     let nameArray = name[indexPath.section] 
//  guard let nameArray = name[indexPath.section] else { 
//   print("Invalid ") 
//   cell.textLabel?.text = "Not Found" 
//   return cell 
//  } 

     cell.textLabel?.text = nameArray![indexPath.row] 
     return cell 
    } 
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     var Keys = [Int] (name.keys) 
     Keys.sortInPlace() 
     let value = Keys[section] 
     var sec = "" 
     if value == Keys[section] { 
      sec = sectionNames[section] 
     } 
     return sec 

    } 
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

     tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "AddItemSegueID" { 
      // let navcontroller = segue.destinationViewController as! UINavigationController 
      // let controller = navcontroller.topViewController as! AddContactViewController 
      let controller = segue.destinationViewController as! AddContactViewController 
      controller.delegate = self 
     } 
    } 

    func addContactViewController(controller: AddContactViewController, didFinishAdding item: ContactNames) { 

     //var dictKeys = [Int](name.keys) 
     let newName = item.text 
     let firstIndex = newName.startIndex 
     //let firstChar = newName[firstIndex] 
     var check = name[0]! 

     let newRowIndex = check.count 

     check.append(newName) 
     tableView.beginUpdates() 
     let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0) 
     let indexPaths = [indexPath] 
     tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic) 
     tableView.endUpdates() 

     dismissViewControllerAnimated(true, completion: nil) 
    } 
    func addContactViewControllerDidCancel(controller: AddContactViewController) { 
     dismissViewControllerAnimated(true, completion: nil) 
    } 
} 


ContactNames是正常的迅速類coontains一個字符串屬性。 另一件事是我實現的協議在AddItemVC以便將數據發送回透視圖控制器,所以我由代表這也是我最近了解到。 Heres yhe addItemVC:

protocol AddContactViewControllerDelegate: class { 

    func addContactViewControllerDidCancel(controller: AddContactViewController) 
    func addContactViewController(controller: AddContactViewController, didFinishAdding item: ContactNames) 
} 

class AddContactViewController: UIViewController, UITextFieldDelegate { 


    weak var delegate: AddContactViewControllerDelegate? 

    @IBOutlet weak var nameField: UITextField! 

    @IBOutlet weak var doneOutlet: UIBarButtonItem! 

    var contactList = ContactsListViewController() 
    override func viewDidLoad() { 
     nameField.becomeFirstResponder() 
     self.tabBarController?.tabBar.hidden = true 
     doneOutlet.enabled = false 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     nameField.resignFirstResponder() 

    } 
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
     let oldText: NSString = textField.text! 
     let newText: NSString = oldText.stringByReplacingCharactersInRange(range, withString: string) 
     doneOutlet.enabled = (newText.length>0) 
//  if newText.length > 0 { 
//   doneOutlet.enabled = true 
//  }else { 
//   doneOutlet.enabled = false 
//  } 

     return true 
    } 

    @IBAction func saveAction(sender: UIBarButtonItem) { 
     let newItem = ContactNames() 
     newItem.text = nameField.text! 

     delegate?.addContactViewController(self, didFinishAdding: newItem) 

    } 
    @IBAction func cancelAction(sender: UIBarButtonItem) { 
     delegate?.addContactViewControllerDidCancel(self) 
    } 
} 

此外,如果你建議一個更好的數據結構。

+0

解決方案我:「?controller.navigationController .popViewControllerAnimated(真)」與發現dismissviewcontroller – amber

回答

0

斯威夫特本機集合類型是值類型,這意味着當你爲一個鍵或索引得到的值對象總是被複制。

分配給變量check數組爲「0」鍵,並附加價值

var check = name[0]! 
... 
check.append(newName) 

但也不變化在字典name原始數組。 您需要將數組分配回字典

name[0] = check 

是不是應該用實際的字母鍵,而不是無關的數字更方便?

+0

取代:試過了太wasnt工作同樣的錯誤。在調試過程中dismissviewcontroller沒有得到調用不知道嘗試controller.dismiss ...。解決方案是navigationController.popViewControllerAnimated(true)。謝謝:) – amber

0

beginUpdates和endUpdates之間,你需要更新你的數據源,使得它的操作相匹配像insertRowsAtIndexPaths。所以,如果你有四行,並且像你一樣插入一行,那麼你的數據源也必須改爲現在有五行。正如錯誤消息所述。

+0

實際上'begin/endUpdates'在這種情況下根本沒有任何作用(單插入操作)並且可以省略。在調用'insertRowsAt ...' – vadian

+0

之前,只需更新數據源數組**即可。那我該如何更新數據源呢?你的意思是我的字典嗎?我發現這個方法用於更新字典。 'updateValue(forKey :)'但即使如此,我該如何動態地做到這一點?我想出了更多的方法'tableView.reloadData()',它給出了相同的錯誤 – amber

相關問題