简体   繁体   English

'NSInternalInconsistencyException',原因:“试图将第4行插入第0节,但更新后第0节只有4行”

[英]'NSInternalInconsistencyException', reason: 'attempt to insert row 4 into section 0, but there are only 4 rows in section 0 after the update'

Ok I've seen previous questions in obj C and tried them. 好的,我已经在obj C中看到了先前的问题并尝试过。 What i'm trying to build is a Contacts app similar to iphone one. 我正在尝试构建的是类似于iPhone One的Contacts应用程序。 I'm having this problem in inserting row in my table. 我在表格中插入行时遇到了这个问题。 Earlier before I never had this problem with inserting rows I probably think it is because of my bad data structure? 在我之前从未遇到过插入行的问题之前,我可能认为这是因为我的数据结构不好吗?

The solution I tried was tableView.beginUpdates and endUpdates but still same error. 我尝试的解决方案是tableView.beginUpdates和endUpdates,但仍然是相同的错误。 Here's my code for contact list. 这是我的联系人列表代码。

    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 is normal swift class that coontains a string property. ContactNames是普通的swift类,包含字符串属性。 The other thing is i implemented a protocol in AddItemVC in order to send data back to pervious view controller so i made a delegate which i also learnt recently. 另一件事是我在AddItemVC中实现了一个协议,以便将数据发送回先前的视图控制器,因此我做了一个委托,这也是我最近学到的。 Heres yhe addItemVC: 这里是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)
    }
}

Also if you suggest a better data structure for this. 另外,如果您为此建议一个更好的数据结构。

The native collection types in Swift are value types that means the objects are always copied when you get a value for a key or for an index. Swift中的本机集合类型是值类型,这意味着在获取键或索引的值时始终会复制对象。

You assign the array for key "0" to variable check and append a value 您将键“ 0”的数组分配给变量check并附加一个值

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

but that does not change the original array in dictionary name . 但这不会更改字典name的原始数组。 You need to assign the array back to the dictionary 您需要将数组分配回字典

name[0] = check

Isn't it more convenient to use the actual letters for the keys rather than unrelated numbers? 将实际字母用作键而不是不相关的数字是否更方便?

Between beginUpdates and endUpdates, you need to update your data source so that it matches the operations like insertRowsAtIndexPaths. 在beginUpdates和endUpdates之间,您需要更新数据源,以使其与insertRowsAtIndexPaths等操作匹配。 So if you had four rows, and insert one as you do, your data source must also be changed to now have five rows. 因此,如果您有四行,并且像这样插入一行,那么还必须将数据源更改为现在有五行。 Exactly as the error message says. 完全如错误消息所述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSInternalInconsistencyException',原因:'尝试将第0行插入第0部分,但更新后第0部分只有0行' - NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' &#39;NSInternalInconsistencyException&#39;,原因:“试图将第0行插入第0节,但更新后第0节只有0行” - 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' “尝试将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' 尝试将第0行插入第0部分,但更新后第0部分只有0行 - attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行 - attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update Swift-“尝试将第0行插入第0节,但更新后第0节只有0行” - Swift - “attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” “尝试将第0行插入第0部分,但更新后第0部分只有0行”错误 - “Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update” Error 尝试插入行时“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行” - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update" when trying to insert row 在表中插入一行时,“试图将第0行插入第0部分,但更新后第0部分只有0行” - 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' when inserting a row into a table 拖放时出现错误“尝试将第 4 行插入第 1 节,但更新后第 1 节中只有 4 行” - Getting an error "attempt to insert row 4 into section 1, but there are only 4 rows in section 1 after the update" while drop and drag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM