具有分层类别的 UITableView -Swift iOS

UITableView with Hierarchical Categories -Swift iOS

我有 3 个类别:

  1. 运动鞋
  2. 裤子
  3. 衬衫

每个类别可以有多个不同的品牌,例如

1.Sneakers > 耐克、阿迪达斯、锐步

2.Pants > 李维斯、李维斯

3.Shirts > 拉尔夫·劳伦、阿伯克隆比

我的用户从一个类别中挑选,然后他们可以添加任意数量的名牌。在他们选择了类别并输入了他们想要的任何品牌名称之后,我需要 2 个 tableView(或 collectionView)。第一个 tableView 应该显示他们选择的类别,第二个 tableView 应该显示他们在该类别中命名的品牌。

我的问题是按类别排序。我知道如何显示他们选择的所有名称,但不知道导致这些名称的类别。

我遇到的问题是,如果用户选择衬衫类别两次,显示它的 tableView 会显示衬衫类别两次,而它应该只显示一次。

我从 Apple 找到了 this,但它没有显示 Swift 中的示例。

Swift 中任何类似的 Whosebug q/a 都可以作为答案。

如果您的数组包含重复的类别。 我希望它必须包含重复项,因为您可以在 table 视图中看到重复项,因此请使用 NSOrderedSet 删除重复项。

这是演示 NSOrderedSet

使用的演示代码

Objective C 版本:

NSArray *category = [NSArray arrayWithObjects:@"Sneakers", @"Pants", @"Shirts",@"Pants", @"Shirts", nil];

NSLog(@"Before using ordered set : %@", category.description);

// iOS 5.0 and later
NSArray * newCategoryArray = [[NSOrderedSet orderedSetWithArray:category] array];

NSLog(@"After using ordered set  : %@", newCategoryArray.description);

Swift版本:

let category = ["Sneakers", "Pants", "Shirts","Pants", "Shirts"];
NSLog("Before using ordered set : %@", category.description);

let newCategoryArray = NSOrderedSet(array: category).array;

NSLog("After using ordered set  : %@", newCategoryArray.description);

输出:

Before using ordered set : (
    Sneakers,
    Pants,
    Shirts,
    Pants,
    Shirts
)

After using ordered set  : (
    Sneakers,
    Pants,
    Shirts
)

编码愉快...