我正在阅读来自网络服务器的文章,这些文章采用 JSON 格式,并在表格视图中显示它们。我想实现部分标题,每个部分都是一个主题。到目前为止只有 3 个主题,下面的“numberOfSectionsInTableView”中的代码正确地确定了有 3 个部分。
我的麻烦在于下一个函数“numberOfRowsInSection”。我无法弄清楚如何确定每个部分中的行数。我查看了有关堆栈溢出的其他答案,但没有人帮助我的原因是我有可变数量的部分和行,而大多数其他提问者的部分数量是固定的。
有任何想法吗?
仅供参考“新闻”是 JSON 格式的文章数组。我遵循这个很棒的教程:JSON
我的头文件如下所示:
NSDictionary *newsArticle;
NSArray *news;
NSMutableData *data;
NSString *tempTopic;
NSString *lastTempTopic;
NSMutableArray *topicList;
我的实现文件如下所示:
- (int)numberOfSectionsInTableView:(UITableView *)tableView{
lastTempTopic = @"";
int numTopics = 0;
topicList = [[NSMutableArray alloc] init];
for (int i=0; i<news.count; i++) {
newsArticle = [news objectAtIndex:i];
tempTopic = [newsArticle objectForKey:@"topic"];
if ([tempTopic isEqualToString:lastTempTopic])
{
}
else
numTopics++;
lastTempTopic = tempTopic;
}
return numTopics;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [news count];
}
这是我正在解析的 JSON 代码的通用位。我希望主题 1、2、3 成为具有适当行数的相应部分。
[{"title":"title1","original_url":"http://originalurl1.html","topic":"主题 1","display_url":"display.url1","author":"Author1" ,"published_on":"date1","text_blocks":[{"TextBlock":"文章内容 1"}]},{"title":"title2","original_url":"http://originalurl2.html" ,"topic":"Topic 1","display_url":"display.url2","author":"Author2","published_on":"date2","text_blocks":[{"TextBlock":"文章内容2 "}]},{"title":"title3","original_url":"http://originalurl3.html","topic":"主题 1","display_url":"display.url3","author":"Author3","published_on":"date3","text_blocks":[{"TextBlock":"文章内容 3"}]},{"title":" title4","original_url":"http://originalurl4.html","topic":"主题 2","display_url":"display.url4","author":"Author4","published_on":"date4 ","text_blocks":[{"TextBlock":"文章内容4"}]}, {"title":"title5","original_url":"http://originalurl5.html","topic":"Topic 2","display_url":"display.url5","author":"Author5","published_on":"date5","text_blocks":[{"TextBlock":"文章内容 5"}]} {"title":"title6","original_url":"http://originalurl6.html","topic":"Topic 2","display_url":"display. url6","author":"Author6","published_on":"date6","text_blocks":[{"TextBlock":"文章内容 6"}]} {"title":"title7","original_url": "http://originalurl7.html","topic":"主题 3","display_url":"display.url7","author":"Author7","published_on":"date7","text_blocks":[ {"TextBlock":"文章内容 7"}]}]"http://originalurl6.html","topic":"主题 2","display_url":"display.url6","author":"Author6","published_on":"date6","text_blocks":[ {"TextBlock":"文章内容 6"}]} {"title":"title7","original_url":"http://originalurl7.html","topic":"Topic 3","display_url":" display.url7","author":"Author7","published_on":"date7","text_blocks":[{"TextBlock":"文章内容 7"}]}]"http://originalurl6.html","topic":"主题 2","display_url":"display.url6","author":"Author6","published_on":"date6","text_blocks":[ {"TextBlock":"文章内容 6"}]} {"title":"title7","original_url":"http://originalurl7.html","topic":"Topic 3","display_url":" display.url7","author":"Author7","published_on":"date7","text_blocks":[{"TextBlock":"文章内容 7"}]}]text_blocks":[{"TextBlock":"文章内容 6"}]} {"title":"title7","original_url":"http://originalurl7.html","topic":"主题 3"," display_url":"display.url7","author":"Author7","published_on":"date7","text_blocks":[{"TextBlock":"文章内容 7"}]}]text_blocks":[{"TextBlock":"文章内容 6"}]} {"title":"title7","original_url":"http://originalurl7.html","topic":"主题 3"," display_url":"display.url7","author":"Author7","published_on":"date7","text_blocks":[{"TextBlock":"文章内容 7"}]}]