iOS:编辑表格中的项目查看问题

iOS:编辑表格中的项目查看问题,ios,swift,uitableview,Ios,Swift,Uitableview,因此,我试图为tableview中的行编写删除编辑行为。但是,当我在选择一行后按delete键时,该行不会从tableView中删除。当我第二次尝试这样做时,我得到一个错误,说找到了一个意外的nil值 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPat

因此,我试图为tableview中的行编写删除编辑行为。但是,当我在选择一行后按delete键时,该行不会从tableView中删除。当我第二次尝试这样做时,我得到一个错误,说找到了一个意外的nil值

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {   // Handle the Delete action
        // Obtain the name of the genre of movie to be deleted
        let genre: String = genres[indexPath.section]

        // Obtain the list of movies in the genre as AnyObject
        let movies: AnyObject? = applicationDelegate.dict_Genres_dict2[genre]
        let movArray: [String] = movies?.allKeys as! [String] //The nil value is unwrapped on this line
        // Typecast the AnyObject to Swift array of String objects
        var moviesOfGenre: Array<String> = movArray 

        // Delete the identified movie at row
        moviesOfGenre.removeAtIndex(indexPath.row)

        if moviesOfGenre.count == 0 {
            // If no movie remains in the array after deletion, then we need to also delete the genre
            applicationDelegate.dict_Genres_dict2.removeObjectForKey(genre)

            // Since the dictionary has been changed, obtain the genre names again
            genres = applicationDelegate.dict_Genres_dict2.allKeys as! [String]

            // Sort the genre names within itself in alphabetical order
            genres.sortInPlace { $0 < $1 }
        }
        else {
            // At least one more movie remains in the array; therefore, the genre stays.

            // Update the new list of movie for the genre in the NSMutableDictionary
            applicationDelegate.dict_Genres_dict2.setValue(moviesOfGenre, forKey: genre)
        }

        // Reload the rows and sections of the Table View 
        tableView.reloadData()
    }
}
override func tableView(tableView:UITableView,committeeditingstyle编辑样式:UITableView单元格编辑样式,forRowAtIndexPath索引路径:nsindepath){
如果editingStyle=.Delete{//处理删除操作
//获取要删除的电影类型的名称
let genre:String=genres[indexPath.section]
//以AnyObject的形式获取该类型电影的列表
让电影:AnyObject?=applicationelegate.dict\u Genres\u dict2[genre]
让movArray:[String]=movies?.allKeys as![String]//在这一行上展开nil值
//将AnyObject类型转换为字符串对象的Swift数组
var moviesOfGenre:Array=movArray
//删除第行中标识的电影
moviesOfGenre.removateIndex(indexPath.row)
如果moviesOfGenre.count==0{
//如果删除后没有电影保留在数组中,那么我们还需要删除类型
applicationelegate.dict_Genres_dict2.removeObjectForKey(类型)
//由于词典已更改,请再次获取流派名称
genres=applicationelegate.dict\u genres\u dict2.allKeys as![String]
//按字母顺序对其内部的流派名称进行排序
tyres.sortInPlace{$0<$1}
}
否则{
//至少还有一部电影保留在阵列中;因此,类型保持不变。
//更新NSMutableDictionary中该类型的新电影列表
applicationelegate.dict_Genres_dict2.setValue(电影:类型)
}
//重新加载表视图的行和节
tableView.reloadData()
}
}

我已经标记了接收到零值的行。任何朝着正确方向的推动都是最有帮助的。谢谢

删除数组项后。您还需要从表中删除该项。当您再次尝试删除该数组项时,它显示为nil,因为该项不可用,但表未从视图中删除该项。因此您也需要从表中删除该项

        genres.removeAtIndex(indexPath.row) // or section , delete according to your app   
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
为什么要删除项目的某个部分。您没有从表中删除流派项目。你只删除了《天使》的电影。您还需要删除表视图项(类型)。从表中删除该类型

您还可以使用滑动删除功能:-

func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
    return true
}

func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
    if (editingStyle == .Delete) {
   // handle delete (by removing the data from your array and updating the tableview)

    self.tableView.beginUpdates()
    self.genres.removeObjectAtIndex(indexPath.row) // also remove an array object if exists.
    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
    self.tableView.endUpdates()
}

我添加了这一点,现在我得到了一个内部不一致异常。我应该如何解释行数/元素数的变化来解决这个问题?您关于从源代码中删除数据的建议很有帮助!非常感谢。