在我从重复的问题中得到任何违规行为之前,我只想说我道歉并且我一直在寻找某种方法来解决这个问题一天,而且我还没有接近任何地方我想去的地方。
所以,我想将一个UITableView行从一个部分移动到另一个部分。所以,例如,我有一个食谱,在"零食"部分,我想把它移到"早餐"部分。我能够移动一次,但是当我再次移动它时,我得到了Array Index Out Of Bounds错误。
我能够在每个部分内移动行而不会崩溃,但是在部分之间移动是我被困住的地方。
修改:错误消息为:致命错误:索引超出范围,并且在"崩溃时让食谱名称跳转到recipesToBeMoved [rowNumberFrom] "
另外,为了使这一点更加清晰,我有4个部分:"早餐","晚餐","午餐","零食" - >这些是来自savedRecipes plist的键(我存储了Yummly API中的所有配方信息)。
到目前为止,这是我的recipesArray看起来像一个条目:
[["Strawberry Jalapeno Pico De Gallo","4","https://lh3.googleusercontent.com/2yZhMGgfnREb2OPcEpzKjHogS2hTClhcRr3OVfp6batG_m4iRWI1cKBKixfv089_yrgblDTKZlq3PFtbPvSWeQ=s90-c", "0g", "0g", "0g", "0g", "No Data", "0mg", "0mg", "26g", "7g", "11g", "3g", "8%", "216%", "9%", "0%", "https://lh3.googleusercontent.com/naElDNklXjL9lxhL2C198_IUshv9aA82LEL_2ZFl3ATAL3m4dRDDTo9DpqeCxBCdR_R6061olbZ2B4LYqh6jRQ=s360", "124", "10 min", "1", "http://www.rhubarbarians.com/strawberry-jalapeno-pico-de-gallo/"]]
这就是recipeNameToMove的样子:
["Strawberry Jalapeno Pico De Gallo", "4", "https://lh3.googleusercontent.com/2yZhMGgfnREb2OPcEpzKjHogS2hTClhcRr3OVfp6batG_m4iRWI1cKBKixfv089_yrgblDTKZlq3PFtbPvSWeQ=s90-c", "0g", "0g", "0g", "0g", "No Data", "0mg", "0mg", "26g", "7g", "11g", "3g", "8%", "216%", "9%", "0%", "https://lh3.googleusercontent.com/naElDNklXjL9lxhL2C198_IUshv9aA82LEL_2ZFl3ATAL3m4dRDDTo9DpqeCxBCdR_R6061olbZ2B4LYqh6jRQ=s360", "124", "10 min", "1", "http://www.rhubarbarians.com/strawberry-jalapeno-pico-de-gallo/"]
这是我的moveRowAtIndexPath方法的代码:
//---------------------------
// Movement of Row Attempted
//---------------------------
func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath)
{
// Obtain the section of the recipe to be deleted
let sectionOfRecipe = sectionTypeArray[fromIndexPath.section]
// Obtain the list of recipes from each section
let recipes: AnyObject? = applicationDelegate.savedRecipesDict[sectionOfRecipe]
// Typecast the anyobject to a swift array
var recipesArray = recipes as! [[String]]
// Row number to move FROM
let rowNumberFrom = fromIndexPath.row
// Row number to move TO
let rowNumberTo = toIndexPath.row
// Section number to move FROM
let sectionNumberFrom = fromIndexPath.section
// Section number to move TO
let sectionNumberTo = toIndexPath.section
// If the recipe cells are moving outside of their sections
if sectionNumberFrom != sectionNumberTo
{
// Where the application crashes on the second move
let recipeNameToMove = recipesArray[rowNumberFrom]
// If movement is from lower part of the list to upper part
if sectionNumberFrom > sectionNumberTo
{
recipesToBeMoved.insert(recipeNameToMove, atIndex: rowNumberTo)
recipesToBeMoved.removeAtIndex(rowNumberFrom + 1)
}
else
{
// Movement is from upper part to lower part
recipesToBeMoved.insert(recipeNameToMove, atIndex: rowNumberTo + 1)
recipesToBeMoved.removeAtIndex(rowNumberFrom)
}
print("Moving out of sections")
print(rowNumberFrom)
applicationDelegate.savedRecipesDict.setValue(recipesToBeMoved, forKey: sectionOfRecipe)
}
}
这就是我如何得到我的部分(即早餐,午餐等)
override func viewDidLoad() {
super.viewDidLoad()
// Adding in rounded corners to the buttons
logCaloriesButton.layer.cornerRadius = 8;
recommendFoodButton.layer.cornerRadius = 8;
sectionTypeArray = applicationDelegate.savedRecipesDict.allKeys as! [String]
// Sort the selection names within itself in alphabetical order
sectionTypeArray.sortInPlace { $0 < $1 }
}
非常感谢你的时间! :)