我有一个NSMutableDictionary包含NSMutableArrays . 字典代表分区,数组代表该分区内的部门 . 我试图从NSMutableArray的内容填充UITableView的单元格 . 我目前有UITableView显示正确数量的部分(分区)和每个部门中正确的单元格数[departmentArray计数];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [divisionArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSArray *temp = [divisionDict objectForKey:[divisionArray objectAtIndex:section]];
return [temp count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *temp = [divisionArray objectAtIndex:section];
if ([temp isEqualToString:@"School of Humanities and Social Sciences"])
{
temp = @"Humanities and Social Sciences";
} else if ([temp isEqualToString:@"School of Science and Mathematics"]) {
temp = @"Science and Mathematics";
} else if ([temp isEqualToString:@"School of Education"]) {
temp = @"Education";
}
return temp;
}
我在cellForRowAtIndexPath中尝试了很多不同的东西来显示每个部门的部门名称,但是我无法让它工作 . 我知道我必须为每个键获取数组,然后通过该数组获取每个部门的名称,但在cellForRowAtIndexPath中实现它会使我感到困惑 .
任何帮助将不胜感激!
2 回答
保持我的结构相同,我能够弄明白,继承我在cellForRowAtIndexPath方法中放置的线:
我认为你最好改变你的结构,让
NSMutableArray
包含NSMutableDictionarys
,其中包含行的NSMutableArrays
和 Headers 的NSStrings
. 我发现在我开发的一些代码中非常方便 .这是如何工作:
你有一个
NSMutableArray
,每个分区都有一个条目,最终成为表格视图中的一个部分 . 在数组的每个条目中,你有一个NSMutableDictionary
,它包含两个条目,一个是我使用了密钥@"rows"
for,包含一个带有行的数组,另一个是我使用了密钥@"title"
,用于保存节头 .然后你的代码变成: