0

How I can delete a JSON data from NSMutableArray that comes from the server.

var jsondata : NSMutableArray!
func retrieve(){
    let requrl: NSURL = NSURL(string: "http://localhost/tara/forvbc.php")!
    let dataurl = NSData(contentsOfURL: requrl)
    jsondata = try! NSJSONSerialization.JSONObjectWithData(dataurl!, options: .MutableContainers) as! NSMutableArray

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = thview.dequeueReusableCellWithIdentifier("thecell", forIndexPath: indexPath) as UITableViewCell
    let asdasd = jsondata[indexPath.row]\
    cell.textLabel?.text = jsondata["name"] as? String
    return cell

}

I like to delete it using commitEditingStyle

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete{

    }
}

Thankyou for your cooperation.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
johnyald
  • 53
  • 5
  • Possible duplicate of [Removing object from NSMutableArray](http://stackoverflow.com/questions/2757046/removing-object-from-nsmutablearray) – Tibrogargan Jul 04 '16 at 22:04
  • Sorry, I think we have only the same title, but this is deleting from an uitable. – johnyald Jul 04 '16 at 22:18
  • The data displayed in your UITable is still backed by the MSMutableArray, which should be independant of the display. It does not matter where your data came from originally, deleting from it would usually be done using either `removeObjectsInArray:` or `removeObjectsAtIndexes:` – Tibrogargan Jul 04 '16 at 22:23
  • @Tibrogargan thankyou sir, It really works – johnyald Jul 04 '16 at 23:09

1 Answers1

0

Adjust the commitEditingStyle code the following way:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
      jsondata.removeAtIndex(indexPath.row)    
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}
meda
  • 45,103
  • 14
  • 92
  • 122
  • Thankyou meda, but I think "jsondata.removeAtIndex(indexPath.row) " works only in Array, it must be "jsondata.removeObjectsAtIndex(indexPath.row) " for I am using NSMutableArray, – johnyald Jul 04 '16 at 23:11