问题原因:
标注颜色的部分用错了方法,removeobject应该换成removeObjectAtIndex
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
var indexpaths = NSArray(array: [indexPath])
if (editingStyle == UITableViewCellEditingStyle.Delete){
self.listteams.removeObject(indexPath.row)
self.tableView.deleteRowsAtIndexPaths(indexpaths, withRowAnimation: UITableViewRowAnimation.Fade)
}else if(editingStyle == UITableViewCellEditingStyle.Insert){
self.listteams.insertObject(self.txtField.text, atIndex: self.listteams.count)
self.tableView.insertRowsAtIndexPaths(indexpaths, withRowAnimation: UITableViewRowAnimation.Fade)
}
self.tableView.reloadData()
}
正确的代码:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
var indexpaths = NSArray(array: [indexPath])
if (editingStyle == UITableViewCellEditingStyle.Delete){
self.listteams.removeObjectAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths(indexpaths, withRowAnimation: UITableViewRowAnimation.Fade)
}else if(editingStyle == UITableViewCellEditingStyle.Insert){
self.listteams.insertObject(self.txtField.text, atIndex: self.listteams.count)
self.tableView.insertRowsAtIndexPaths(indexpaths, withRowAnimation: UITableViewRowAnimation.Fade)
}
self.tableView.reloadData()
}