尝试使用 NSDictionary 值设置 numberOfRowsInSection
我正在尝试设置我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能像对数组那样对字典进行索引。您可以使用键访问字典。 NSDictionary 确实提供了两种获取数组的方法:allKeys 和 allValues。
我不确定你想要完成什么,但如果你想按字母顺序组织汽车制造商,你可以获取所有键([arraysByLetter allKeys]),按字母顺序对它们进行排序,然后索引到该排序的数组中。当您实际获得汽车制造商的名称时,您将使用 objectForKey: 加载制造商数组。
更新:
假设有一个名为sortedLetters 的排序数组,您可以将numberOfRowsInSection 更改为以下内容:
您可能希望在初始化时设置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:
You probably want to setup the sortedLetters array on initialization, unless it is dynamic.
警告消息准确地告诉您问题所在:
arraysByLetter
是NSMutableDictionary
的实例,而不是NSArray
(或其子类NSMutableArray
),因此不会响应NSArray
方法,例如objectAtIndex:
。相反,您应该使用
NSDictionary
方法objectForKey:
,并传递与所提供的节号相对应的字母。您可以使用 switch 语句或字符串数组来确定要选择哪个键。The warning message is telling you exactly what the problem is:
arraysByLetter
is an instance ofNSMutableDictionary
notNSArray
(or its subclass,NSMutableArray
), and therefore doesn't respond toNSArray
methods such asobjectAtIndex:
.Instead, you should be using the
NSDictionary
methodobjectForKey:
, and passing the letter of the alphabet that corresponds to the provided section number. You could use aswitch
statement or an array of strings to figure out which key to pick.objectAtIndex 是一个数组方法,而不是字典方法。我对表数据源使用字典的方式是这样的:
我们必须排序的原因是因为 allKeys 没有特定的顺序出现并且可以改变。
objectAtIndex is an array method, not a dictionary method. The way I use dictionaries for table data sources is as so:
The reason we have to sort is because allKeys appears in no particular order and can change.
这是因为您不能在字典上使用 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.