gpt4 book ai didi

iphone - 尝试使用 NSDictionary 值设置 numberOfRowsInSection

转载 作者:行者123 更新时间:2023-12-01 19:26:06 25 4
gpt4 key购买 nike

我正在尝试设置我的 uitableview 以进行索引,我的部分工作正常,现在数据在 NSDictionary 中,这是我的输出与 nslog --->

Dictionary: {
H = (
Honda,
Honda,
Honda,
Honda,
Honda,
Honda,
Honda
);
M = (
Mazda,
Mazda,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi
);
N = (
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan
);
T = (
Toyota,
Toyota,
Toyota
);

我现在正试图像这样设置我的 table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [arraysByLetter count];
//return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[arraysByLetter objectAtIndex:section] count];
//return 1;
}

但是我收到警告 'NSMutableDictionary' 可能不会响应 'objectAtIndex:' 在我的 numberOfRowsInSection 委托(delegate)方法中......我该如何解决这个问题?

最佳答案

您不能像索引数组那样索引字典。您可以使用键访问字典。 NSDictionary 确实提供了两种获取数组的方法:allKeys 和 allValues。

我不确定您要完成什么,但如果您想按字母顺序组织汽车制造商,您可以获取所有键([arraysByLetter allKeys]),按字母顺序排序,然后索引到该排序数组。当您实际获得汽车制造商的名称时,您将使用 objectForKey: 加载制造商数组。

更新:
假设一个名为 sortedLetters 的排序数组,您可以将 numberOfRowsInSection 更改为以下内容:

NSString *currentLetter = [sortedLetters objectAtIndex:section];
return [[arraysByLetter objectForKey:currentLetter] count];

您可能希望在初始化时设置 sortedLetters 数组,除非它是动态的。

关于iphone - 尝试使用 NSDictionary 值设置 numberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7506877/

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