我正在尝试允许用户在部分的末尾插入行,但是我收到了错误。然而一切似乎都是正确的,所以必须有一些我没有看到的东西。有人能指出我正确的方向。附件是我的代码,错误和表格的截图。
//
// Test.swift
// Table Views
//
// Created by Deion Long on 7/18/15.
// Copyright (c) 2015 Deion Long. All rights reserved.
//
import UIKit
var array = ["Section 1", "Section 2"]
extension UITableView {
func indexPathForView (view : UIView) -> NSIndexPath? {
let location = view.convertPoint(CGPointZero, toView:self)
return indexPathForRowAtPoint(location)
}
}
class Test: UIViewController, UITableViewDelegate, UITableViewDataSource {
var things:NSMutableArray = ["hi", "bye", "kie"]
@IBAction func editBtnClicked(sender: AnyObject) {
//When not in editing mode already set editing to true
if(table.editing == false){
self.editing = true
println("hi")
table.setEditing(true, animated: true)
}else{
self.editing = false
table.setEditing(false, animated: true)
}
}
@IBOutlet weak var table: UITableView!
@IBAction func doneButton(sender: AnyObject) {
table.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
table.allowsSelectionDuringEditing = true
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return array.count // This was put in mainly for my own unit testing
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return array[section]
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var addRow = self.editing ? 1 : 0
println(addRow)
return addRow + things.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("textInputCell3") as! DetailCell
if (indexPath.row >= things.count && self.editing) {
cell.configure(text: "Add Row", placeholder: "Enter")
} else{
cell.configure(text: things.objectAtIndex(indexPath.row) as! String, placeholder: "Enter")
}
return cell
}
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.table.setEditing(editing, animated: animated)
if(editing){
table.beginUpdates()
for var i = 0; i < self.table.numberOfSections(); i++
{
var lastRow = table.numberOfRowsInSection(i)
var lastIndex = NSIndexPath(forRow: lastRow, inSection: i)
table.insertRowsAtIndexPaths([lastIndex], withRowAnimation: UITableViewRowAnimation.Automatic)
}
table.endUpdates()
}
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if(indexPath.row >= things.count){
return UITableViewCellEditingStyle.Insert
}else{
return UITableViewCellEditingStyle.Delete
}
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
things.removeObject(indexPath.row)
table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
else if (editingStyle == UITableViewCellEditingStyle.Insert) {
things.insertObject("123", atIndex: self.things.count)
table.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
}
}
错误:由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第1节中的行数无效。更新后的现有部分中包含的行数(5)必须等于数字在更新(4)之前包含在该部分中的行,加上或减去从该部分插入或删除的行数(0插入,0删除)和加或减移入或移出该部分的行数(0搬进来,0搬出去。'
答案 0 :(得分:0)
错误消息说明了一切。在更新模型数据以获得额外行之前,不得插入行。你可能会在这里混淆自己的部分;首先练习一节。您的模型似乎没有很好地区分这两个部分。
答案 1 :(得分:0)
在setEditing:
中,您在UITableView的beginUpdates和endUpdates函数中插入行
table.beginUpdates()
for var i = 0; i < self.table.numberOfSections(); i++ {
var lastRow = table.numberOfRowsInSection(i)
var lastIndex = NSIndexPath(forRow: lastRow, inSection: i)
table.insertRowsAtIndexPaths([lastIndex], withRowAnimation: UITableViewRowAnimation.Automatic)
}
table.endUpdates()
插入行时,请确保更新模型对象things
。如果您只想重新加载单元格的数据,请使用
table.reloadRowsAtIndexPaths([lastIndex], withRowAnimation: UITableViewRowAnimation.Automatic)