При удалении строки из tableView, появлятся ошибка ниже. (Удаляю свайпом, по стандарту)
Я постепенно осваиваю этот язык и столкнулся с такой бедой:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5),
plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted)
and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Сам код вот:
import UIKit
class RemoteAPI {
func getData(completionHandler: ((NSArray!, NSError!) -> Void)!) -> Void {
let url: NSURL = NSURL(string: "http://site.ru")!
let ses = NSURLSession.sharedSession()
let task = ses.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
if (error != nil) {
return completionHandler(nil, error)
}
var error: NSError?
let json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSDictionary
if (error != nil) {
return completionHandler(nil, error)
} else {
return completionHandler(json["results"] as! [NSDictionary], nil)
}
})
task.resume()
}
}
var api = RemoteAPI()
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var refreshControl:UIRefreshControl!
var person: NSMutableArray = []
func showForm(id:NSString){
let toShow = self.storyboard?.instantiateViewControllerWithIdentifier(id as String) as! ViewController2
self.navigationController?.pushViewController(toShow, animated: true)
}
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refreshControl)
api.getData({data, error -> Void in
if (data != nil) {
self.person = NSMutableArray(array: data)
self.tableView!.reloadData()
} else {
println("api.getData failed")
println(error)
}
})
self.tableView.delegate = self
self.tableView.dataSource = self
delay(0.5) {
self.tableView!.reloadData()
}
}
func refresh(sender:AnyObject)
{
api.getData({data, error -> Void in
if (data != nil) {
self.person = NSMutableArray(array: data)
self.tableView!.reloadData()
} else {
println("api.getData failed")
println(error)
}
})
self.refreshControl.endRefreshing()
delay(0.5) {
self.tableView!.reloadData()
}
}
@IBAction func refreshBtn(sender: AnyObject) {
self.refresh(0);
self.tableView!.reloadData()
}
@IBAction func showBtn(sender: AnyObject) {
showForm("lol");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var storyBoard = UIStoryboard(name: "Main", bundle: nil)
var secondVC = storyBoard.instantiateViewControllerWithIdentifier("lol") as! ViewController2
secondVC.l1 = person[indexPath.row]["name"] as! String
secondVC.l2 = person[indexPath.row]["date"] as! String
secondVC.l3 = person[indexPath.row]["priem1"] as! String
secondVC.l4 = person[indexPath.row]["priem2"] as! String
secondVC.l5 = person[indexPath.row]["priem3"] as! String
secondVC.l6 = person[indexPath.row]["diagnoz"] as! String
secondVC.l7 = person[indexPath.row]["cena"] as! String
secondVC.l8 = person[indexPath.row]["predoplata"] as! String
secondVC.l9 = person[indexPath.row]["sms"] as! String
self.navigationController?.pushViewController(secondVC, animated: true)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return person.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let row = indexPath.row
let fullname = self.person[indexPath.row]["name"] as? NSString
let date = self.person[indexPath.row]["date"] as? NSString
(cell.contentView.viewWithTag(1) as! UILabel).text = fullname as? String
(cell.contentView.viewWithTag(2) as! UILabel).text = date as? String
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete {
//person.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
}