循环访问 cellForRowAtIndexPath 处的字典数组

发布于 2024-12-04 21:02:26 字数 448 浏览 0 评论 0原文

我有一系列字典。每本词典都有一个类别。我想用所有具有一个共同类别的字典填充一个表格视图,该类别是在上一个表格视图中选择的。

NSDictionary *name = [sortedNames objectAtIndex:indexPath.row];
NSMutableString *currentCat = [name objectForKey:@"category"];

if ([currentCat isEqualToString:catSelected]) {
    cell.textLabel.text = [name objectForKey:@"title"];
}

正如您可能已经猜到的,如果数组中的前两个字典不是所选类别的类型,但第三个是,则第三个单元格行将被填充 ?

我该如何以正确的方式处理这个问题

I have an array of dictionaries. Each dictionary has one category. I want to fill a tableview with all of the dictionaries with one category in common, which was selected in a previous tableview.

NSDictionary *name = [sortedNames objectAtIndex:indexPath.row];
NSMutableString *currentCat = [name objectForKey:@"category"];

if ([currentCat isEqualToString:catSelected]) {
    cell.textLabel.text = [name objectForKey:@"title"];
}

As you could've guessed, if the first two dictionaries in the array are not of the category selected type, but the third one is, then the third cellrow gets filled in.

How do I approach this the right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白鸥掠海 2024-12-11 21:02:26

在 .h 文件中获取一个可变数组在 viewWillAppear init 中并使用您的代码为该数组添加对象,例如

  array = [[NSMutableArray alloc] init];
for(int i = 0;i<[sortedNames count];i++)
{
    NSDictionary *name = [sortedNames objectAtIndex:i];
    NSMutableString *currentCat = [name objectForKey:@"category"];

    if ([currentCat isEqualToString:catSelected]) {
        [array addObject:[name objectForKey:@"title"]];
    }
}

In numberOfRowsInSection 方法给出

[array count];

In cellForRowAtIndexPath 方法

cell.textLabel.text =[array objectAtIndex:indexPath.row];

take a mutable array in .h file In viewWillAppear init and add objects for that array using your code like

  array = [[NSMutableArray alloc] init];
for(int i = 0;i<[sortedNames count];i++)
{
    NSDictionary *name = [sortedNames objectAtIndex:i];
    NSMutableString *currentCat = [name objectForKey:@"category"];

    if ([currentCat isEqualToString:catSelected]) {
        [array addObject:[name objectForKey:@"title"]];
    }
}

In numberOfRowsInSection method give

[array count];

In cellForRowAtIndexPath method

cell.textLabel.text =[array objectAtIndex:indexPath.row];
羅雙樹 2024-12-11 21:02:26

您可以在加载视图控制器时构造一个 NSMutableArray 。该数组仅包含您想要在该表视图中显示的对象。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.namesArray = [NSMutableArray array];
    for (NSDictionary *dict in sortedNames) {
        if ([(NSString *)[dict objectForKey:@"category"] isEqualToString:catSelected]) {
            [newArray addObject:dict];
        }
    }
}

然后,在 tableView:cellForRowAtIndexPath: 中,根据索引进行分配:

if (indexPath.row < [namesArray count]) {    // Just incase...
    cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];
}

这里的关键思想是,我们不会引入表视图不需要的数据。

You could construct an NSMutableArray on loading your view controller. This array would only contain the objects you want to display in that table view.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.namesArray = [NSMutableArray array];
    for (NSDictionary *dict in sortedNames) {
        if ([(NSString *)[dict objectForKey:@"category"] isEqualToString:catSelected]) {
            [newArray addObject:dict];
        }
    }
}

Then, in your tableView:cellForRowAtIndexPath:, you assign based on the index:

if (indexPath.row < [namesArray count]) {    // Just incase...
    cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];
}

The key idea here is that we don't bring data we don't need for the table view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文