Answer the question
In order to leave comments, you need to log in
Why is the row and UITableView not removed?
When deleting a row from a tableView, the error below appears. (I delete it with a swipe, according to the standard)
I am gradually mastering this language and ran into such a disaster:
*** 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)
}
}
}
Answer the question
In order to leave comments, you need to log in
Everything is written in the text of the error - after the update, the section should have the number of lines equal to the number before the update, minus the number of deleted ones. You need to change the datasource inside updates, something like this:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == UITableViewCellEditingStyle.Delete
{
self.tableView.beginUpdates()
person.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.tableView.endUpdates()
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question