尝试使用 NSDictionary 值设置 numberOfRowsInSection

发布于 2024-12-06 03:21:40 字数 1176 浏览 0 评论 0原文

我正在尝试设置我的 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
    );

我现在尝试像这样设置我的表

- (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' 可能不会响应我的 numberOfRowsInSection 委托方法中的 'objectAtIndex:' ...我该如何解决这个问题?

I am trying to set up my uitableview for indexing, I have got number of sections working fine, now and the data is in the NSDictionary heres my output with 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
    );

I am now trying to set up my table like so

- (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;
}

however I am getting a warning 'NSMutableDictionary' may not respond to 'objectAtIndex:' inside my numberOfRowsInSection delegate method... How do I get around this?

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

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

发布评论

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

评论(4

假面具 2024-12-13 03:21:40

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

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

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

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

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

You cannot index a dictionary as you would an array. You access dictionaries using keys. NSDictionary does provide two methods for getting arrays: allKeys and allValues.

I'm not sure what you are trying to accomplish, but if you want to organize the car makers alphabetically, you can get all the keys ([arraysByLetter allKeys]), sort them alphabetically, and then index into that sorted array. When you actually get the car makers' names you will then use objectForKey: to load the array of makers.

Update:
Assuming a sorted array named sortedLetters, you can change your numberOfRowsInSection to the following:

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

You probably want to setup the sortedLetters array on initialization, unless it is dynamic.

锦上情书 2024-12-13 03:21:40

警告消息准确地告诉您问题所在: arraysByLetterNSMutableDictionary 的实例,而不是 NSArray (或其子类 NSMutableArray),因此不会响应 NSArray 方法,例如 objectAtIndex:

相反,您应该使用 NSDictionary 方法 objectForKey:,并传递与所提供的节号相对应的字母。您可以使用 switch 语句或字符串数​​组来确定要选择哪个键。

The warning message is telling you exactly what the problem is: arraysByLetter is an instance of NSMutableDictionary not NSArray (or its subclass, NSMutableArray), and therefore doesn't respond to NSArray methods such as objectAtIndex:.

Instead, you should be using the NSDictionary method objectForKey:, and passing the letter of the alphabet that corresponds to the provided section number. You could use a switch statement or an array of strings to figure out which key to pick.

情独悲 2024-12-13 03:21:40

objectAtIndex 是一个数组方法,而不是字典方法。我对表数据源使用字典的方式是这样的:

- (NSInteger)tableView:numberOfRowsInSection:
{
NSArray *keys = [arraysByLetter allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveComapre:)];

return [[arraysByLetter objectForKey:[sortedKeys objectAtIndex:section]] count];
}

我们必须排序的原因是因为 allKeys 没有特定的顺序出现并且可以改变。

objectAtIndex is an array method, not a dictionary method. The way I use dictionaries for table data sources is as so:

- (NSInteger)tableView:numberOfRowsInSection:
{
NSArray *keys = [arraysByLetter allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(caseInsensitiveComapre:)];

return [[arraysByLetter objectForKey:[sortedKeys objectAtIndex:section]] count];
}

The reason we have to sort is because allKeys appears in no particular order and can change.

帝王念 2024-12-13 03:21:40

这是因为您不能在字典上使用 objectAtIndex。您需要首先使用字典上的 objectForKey 提取字典中的对象,然后从这些对象创建一个数组。然后,您可以在该数组(数组对象的数组)上使用 objectAtIndex 并在结果上使用 count。或者向您的字典发送简单的 allValues 消息也足够了。

编辑:

在您的情况下,如果您需要第一部分有七行(七个本田),请创建一个数组并将 [dictionary allValues] 作为其内容。在 numberOfRowsInSection 方法中使用该数组,就像现在使用字典一样。

That is because you cannot use objectAtIndex on a dictionary. You need to first extract the objects on your dictionary using objectForKey on your dictionary and then create an array from those objects. Then you may use objectAtIndex on that array (array of array objects) and use count on the result. Or a simple allValues message to your dictionary would also suffice.

EDIT:

In your case if you need your first section to have seven rows (seven Hondas), create an array and place [dictionary allValues] as its content. Use that array inside your numberOfRowsInSection method like you do now with your dictionary.

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