如何用 NSDictionary 值填充 UITableViewCell TextLabels?

发布于 2024-12-05 17:28:21 字数 1499 浏览 0 评论 0原文

我正处于设置 Indexed Uitableview 以进行快速滚动的最后阶段,一旦完成此操作,我将写下我的经验,希望能帮助任何试图实现与我相同结果的人。 不过,我还有最后一步将 NSDictionary 值传递到我的 tableviewcells 文本标签中。我只是不确定如何做到这一点,希望有人能为我提供一个示例。

这就是我的字典的样子

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
    );

我尝试了一些随机的事情,但对于将 NSDictionary 传递给 uitableview 几乎一无所知...这是我在 tableView:cellForRowAtIndexPath:

//..
[[cell textLabel] setText:[[arraysByLetter objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];

    return cell;
//..

arraysByLetter 中尝试过的内容是我的 NSDictionary

并且代码行给出我这个警告 'NSMutableDictionary' 可能不会响应 'objectAtIndex:'

任何帮助将不胜感激

UPDATE:::

我找到了一个很好的例子,说明如何使用填充 uitableview 单元格文本标签像 arraysByLetter 一样创建的 NSString

NSString *value = [arraysByLetter objectForKey:[[arraysByLetter allKeys] objectAtIndex:indexPath.row]];
    cell.textLabel.text = value;

是我排序的 NSDictionary,但这导致程序收到 信号“EXC_BAD_ACCESS” 不知道为什么,但试图解决 它。

I am on the last stage of setting up my Indexed Uitableview for fast scrolling, Once I have finished this I am going to write about my experience to hopefully help anyone that is trying to achieve the same results as myself.
However I have one last step passing my NSDictionary values into my tableviewcells textlabels.. I'm just not sure how to do it and hoping someone can provide me with an example.

This is what my dictionary looks like

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 have tried a few random things but am pretty much clueless about passing a NSDictionary to the uitableview... heres what i Have attempted inside tableView:cellForRowAtIndexPath:

//..
[[cell textLabel] setText:[[arraysByLetter objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];

    return cell;
//..

arraysByLetter is my NSDictionary

and the line of code is giving me this warning
'NSMutableDictionary' may not respond to 'objectAtIndex:'

any help would be greatly appreciated

UPDATE:::

I have found a great example of how to populate the uitableview cell textlabel with a NSString that is created like so

NSString *value = [arraysByLetter objectForKey:[[arraysByLetter allKeys] objectAtIndex:indexPath.row]];
    cell.textLabel.text = value;

arraysByLetter being my sorted NSDictionary, however this is causing a program received signal "EXC_BAD_ACCESS" not sure why but trying to work through it.

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

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

发布评论

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

评论(2

山人契 2024-12-12 17:28:22

您似乎将 NSMutableDictionaryNSMutableArray 混淆了。前者通过key(通常是NSString)访问对象,后者通过NSUIntegers访问对象。

例如,如果 dict 是一个 NSMutableDictionary,那么您可以调用

[dict objectForKey:@"myKey"];

If array is an NSMutableDictionary,那么您可以 对于您给出的示例

[array objectAtIndex:0];

,您有一个 NSDictionary,其中 objectForKey: 是一个 NSArray。因此,您应该调用诸如

[[dict objectForKey:@"H"] objectAtIndex:0];

输出“Honda”或

[[dict objectForKey:@"M"] objectAtIndex:2];

输出“Mitsubishi”之类的内容。

You appear to be confusing an NSMutableDictionary with an NSMutableArray. The former accesses objects by keys, typically NSStrings, and the latter by NSUIntegers.

For example, if dict is an NSMutableDictionary, then you might call

[dict objectForKey:@"myKey"];

If array is an NSMutableDictionary, then you might call

[array objectAtIndex:0];

For the example you give, you have an NSDictionary where the objectForKey: is an NSArray. Therefore you should be calling something like

[[dict objectForKey:@"H"] objectAtIndex:0];

to output "Honda" or

[[dict objectForKey:@"M"] objectAtIndex:2];

to output "Mitsubishi".

倾听心声的旋律 2024-12-12 17:28:22

您正在对字典使用数组方法调用。在字典中,您可以通过使用与该对象(成员)关联的键来获取其成员。在数组中,您可以通过使用数组索引来获取数组的成员。

您正在使用需要键的索引。

You are using an array method call on a dictionary. In a dictionary, you get its members by using a key that is associated with that object (member). In an array, you get a member of the array by using the index into the array.

You are using an index where a key is needed.

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