IOS SWIFT从数组中删除项目在我的TableView中不起作用



我有一个通知类,它有以下函数(ed of post),返回一个数组,我调用下面的函数(getListOfNotifications),并从我的主ViewController中获取一个数据数组。在我的主控制器中,我这样定义阵列:

var arr = []
arr = getListOfNotifications(“(userIdInt)”)

然后在我的prepareForSegue中,我将数组传递给表View

let secondVC = segue.destinationViewController as       NotificationsTableViewController
secondVC.notificationsArray = arr

然后在表视图中,我有以下内容,它是从上面填充的,

var notificationsArray =  []

然后在我的表视图中,当我尝试执行以下删除时,我不能在数组上使用removeObjectAtIndex,但我也不能将数组定义为NSMutableArray。如何从数组中删除该项?我试着以任何可能的方式重新定义它,我可以;I don’我什么都没做。

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
    notificationsArray.removeObjectAtIndex(indexPath.row)
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
 }

这是我从web服务中填充的Notifications类中的函数:

func getListOfNotifications(item:String)->Array<Notifications> {
    var notificationArray:[Notifications] = []
     println("==== Notifications ====")
    var url=NSURL(string:”http://www.mywebsite.com/NotificationListJSON.php?id="+item)
    var data=NSData(contentsOfURL:url!)
    if let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as? NSDictionary {
        if let feed = json["notifications"] as? NSArray {
            for entry in feed {
                 var notifications = Notifications()
                 notifications.desc = entry["description"] as String
                 notifications.name = entry["name"] as String
                 notifications.iid = entry["iid"] as String
                 notifications.distance = entry["distance"] as String
                 notificationArray.append(notifications)
            }
        }
    }
    return notificationArray
}

如果您知道数组将是什么,为什么首先要将其声明为NSArray。为什么不:

var notificationsArray =  [Notifications]()

然后,您将有一个快速的通知数组,它是可变的,您应该能够删除它。

removeObjectAtIndexNSMutableArray的一个方法,但您使用的是Swift数组(声明为var,因此也是可变的)。

使用Swift标准库参考中描述的标准删除方法。

notificationsArray.removeAtIndex[indexPath.row]

最新更新