我有一个表视图,用户也添加了食谱,现在它只是根据他们添加食谱的顺序进行排序。我希望通过配方名称(UILabel)A->对表格视图进行排序。 Z.不确定哪些代码可能有用,但下面是我设置的数据类,UI标签出口在UITableViewCell中名为recipeNameLabel。
class Recipe: NSObject, NSCoding {
var name: String
var servings: String
var category: String
var prepTime: String
var cookTime: String
var source: String
var sourceURL: String
var photo: UIImage?
}
我在UITableView中调用它来检索数据模型:
var recipes2 = Recipe()
答案 0 :(得分:1)
您需要对要用作数据源的阵列进行排序。
假设你有一个Recipe
s数组:
var recipes = [Recipe]()
您可以使用sort
按照name
属性对其进行排序:
recipes = recipes.sort({ current, next in
return current.name < next.name
})