gpt4 book ai didi

ios - 具有分层类别的 UITableView -Swift iOS

转载 作者:行者123 更新时间:2023-11-28 06:34:07 25 4
gpt4 key购买 nike

我有 3 个类别:

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

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

1.运动鞋 > 耐克、阿迪达斯、锐步

2.裤子> Lees, Levi's

3.衬衫>拉尔夫·劳伦、阿伯克龙比

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

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

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

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

Swift 中任何类似的 StackOverflow 问答都可以作为答案。

最佳答案

如果您的数组包含重复的类别。我希望它一定包含重复项,因为您可以在 TableView 中看到重复项,所以使用 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);

快速版本:

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
)

快乐编码......

关于ios - 具有分层类别的 UITableView -Swift iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39536447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com