我正在疯狂地试图弄清楚如何实施tableView numberofRowsinSection
,并希望我能得到一些提示或建议。我觉得我整个事情过于复杂。让我首先发布转换后的xml数据,现在是NSDictionary。
{
"@body" = "";
"@class_id" = "";
"@title" = Discussions;
"@type" = "Discussion Block";
},
{
"@body" = "";
"@class_id" = "";
"@col" = 1;
"@title" = YouTube;
"@type" = "Youtube Block";
item = {
"@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e";
"@title" = "Gavin DeGraw - Not Over You";
"@type" = link;
};
},
{
"@body" = "";
"@class_id" = "";
"@title" = Calendar;
"@type" = "Calendar Block";
},
{
"@body" = "<p> homework test</p> ";
"@class_id" = "";
"@title" = Homework;
"@type" = "File Block";
item = (
{
"@body" = "https://MYDOMAIN.com/securelink;
"@title" = "crashreport.rtf";
"@type" = file;
},
{
"@body" = "mydomain.com/securelink";
"@title" = "Chem notes sep 26.docx";
"@type" = file;
}
);
},
{
"@body" = "<p> Link testing</p> ";
"@class_id" = "";
"@title" = Links;
"@type" = "Link Block";
item = (
{
"@body" = "http://google.com";
"@title" = Link1;
"@type" = link;
},
{
"@body" = "http://yahoo.com";
"@title" = link2;
"@type" = link;
},
{
"@body" = "http://reddit.com";
"@title" = link3;
"@type" = link;
}
);
}
)
以上数据是回复。这样做的目的是让第一个标题键成为表格标题,将第二个“标题”键(在“项目”内)作为各自部分的行。在tableView TitleforHeaderinSection
方法中,我只使用此代码
for (NSDictionary *roster in response) {
[blockName addObject:[roster objectForKey@"@title"]];
}
return [blockName objectAtIndex:section];
numberofRowsinSection
方法让事情变得棘手。这就是我试过的
for (NSDictionary *myDict in [[response objectAtIndex:section] valueForKeyPath:@"item"]) {
NSArray *keys = [myDict allKeys];
if ([keys containsObject:@"@title"]) {
array3 = [[NSMutableArray alloc] init];
[array3 addObject:[myDict objectForKey:@"@title"]];
}
}
return array3;
我得到的错误是myDict是NSString,并且不能在其上使用allKeys。我不确定为什么会发生这种情况,因为当我将其更改为objectAtIndex:1并记录allKeys方法时。我也很确定这不是找到每个部分所需行数的最佳方法,所以如果有更好的方法请分享。
为了澄清一下,在解析上述数据之后,这就是我在tableView中寻找的东西:
Header (Discussions)
-No rows under this header
Header (YouTube)
-Row (Gavin DeGraw - Not Over You)
Header (Calendar)
-No rows
Header (Homework)
-Row (crashreport)
-Row (chem notes)
Header (links)
-Row (link1)
-Row (link2)
-Row (link3)
感谢您的帮助。我已经对NSDictionary和NSArray方法做了很多研究,但我不知道如何解决这个问题或者用其他方式编写代码。
答案 0 :(得分:0)
问题在于:
for([response objectAtIndex:section]中的NSDictionary * myDict valueForKeyPath:@&#34; item&#34;]){
这适用于下面的字典,因为它遍历NSDray对象的NSArray。因此,当调用[myDict allKeys]
时,您实际上是在NSDictionary上调用它。
{
"@body" = "<p> homework test</p> ";
"@class_id" = "";
"@title" = Homework;
"@type" = "File Block";
item = (
{
"@body" = "https://MYDOMAIN.com/securelink;
"@title" = "crashreport.rtf";
"@type" = file;
},
{
"@body" = "mydomain.com/securelink";
"@title" = "Chem notes sep 26.docx";
"@type" = file;
}
);
但是对于下一个字典来说并不好,因为你循环遍历字典而不是数组,所以for..in循环是循环字典的键。然后,您在NSString上调用allKeys
(也就是在字典中的键上)。
{
"@body" = "";
"@class_id" = "";
"@col" = 1;
"@title" = YouTube;
"@type" = "Youtube Block";
item = {
"@body" = "http://www.youtube.com/watch?v=vDWhfsQHq1o&ob=av3e";
"@title" = "Gavin DeGraw - Not Over You";
"@type" = link;
};
},
你可以这样做:
id item = [[response objectAtIndex:section] valueForKeyPath:@"item"];
if(item) {
if([item isKindOfClass:[NSDictionary class]]) {
NSArray *keys = [(NSDictionary *)item allKeys];
} else if([item isKindOfClass:[NSArray class]]) {
for (NSDictionary *myDict in (NSArray *)item) {
NSArray *keys = [myDict allKeys];
}
}
}
else {
return 0;
}
答案 1 :(得分:0)
假设存在“item”键,则相应的对象是项目的NSArray。
尝试:
NSArray *items = [[response objectAtIndex:section] valueForKey:@"item"];
if (items!=nil) {
return [items count];
} else {
return 0;
}