2016-07-04 36 views
0

如何从服务器中删除NSMutableArray中的JSON数据。如何删除NSMutableArray的数据

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 

} 

我喜欢用commitEditingStyle

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

    } 
} 

谢谢你们的合作,将其删除。

+0

[从的NSMutableArray卸下对象(http://stackoverflow.com/questions/2757046/removing-object-from-nsmutablearray) – Tibrogargan

+0

对不起的可能的复制,我认为我们只有相同的标题,但是这是从一个可用的项目中删除。 – johnyald

+0

显示在UITable中的数据仍由MSMutableArray支持,MSMutableArray应与显示无关。不要紧,你的数据来自最初的地方,从它删除通常会使用'removeObjectsInArray:'或'removeObjectsAtIndexes:' – Tibrogargan

回答

0

调整commitEditingStyle代码如下方式:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     jsondata.removeAtIndex(indexPath.row)  
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
    } 
} 
+0

谢谢梅达,但我认为“jsondata.removeAtIndex indexPath.row)“仅适用于Array,它必须是”jsondata.removeObjectsAtIndex(indexPath.row)“,因为我使用NSMutableArray, – johnyald

+0

是的,所以调整代码,你明白了 – meda