如何用 NSDictionary 值填充 UITableViewCell TextLabels?
我正处于设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎将
NSMutableDictionary
与NSMutableArray
混淆了。前者通过key
(通常是NSString
)访问对象,后者通过NSUIntegers
访问对象。例如,如果
dict
是一个NSMutableDictionary
,那么您可以调用If
array
is anNSMutableDictionary
,那么您可以 对于您给出的示例,您有一个
NSDictionary
,其中objectForKey:
是一个NSArray
。因此,您应该调用诸如输出“Honda”或
输出“Mitsubishi”之类的内容。
You appear to be confusing an
NSMutableDictionary
with anNSMutableArray
. The former accesses objects bykey
s, typicallyNSString
s, and the latter byNSUIntegers
.For example, if
dict
is anNSMutableDictionary
, then you might callIf
array
is anNSMutableDictionary
, then you might callFor the example you give, you have an
NSDictionary
where theobjectForKey:
is anNSArray
. Therefore you should be calling something liketo output
"Honda"
orto output
"Mitsubishi"
.您正在对字典使用数组方法调用。在字典中,您可以通过使用与该对象(成员)关联的键来获取其成员。在数组中,您可以通过使用数组索引来获取数组的成员。
您正在使用需要键的索引。
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.