Ios 创建用于计算numberOfRowsInSection的NSDictionary
我正在尝试为我的Ios 创建用于计算numberOfRowsInSection的NSDictionary,ios,uitableview,Ios,Uitableview,我正在尝试为我的uiTableView正确显示节和行 我从一位投稿人那里得到了很大的帮助,并且已经接近解决我的问题。可以看出这个问题。这离正确不远,只是需要分类的部分 它是重复章节标题,而不是只显示一次。我不知道该怎么解决这个问题 // Find out the path of recipes.plist NSString *path = [[NSBundle mainBundle] pathForResource:@"lawpolice" ofType:@"plist"];
uiTableView
正确显示节和行
我从一位投稿人那里得到了很大的帮助,并且已经接近解决我的问题。可以看出这个问题。这离正确不远,只是需要分类的部分
它是重复章节标题,而不是只显示一次。我不知道该怎么解决这个问题
// Find out the path of recipes.plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"lawpolice" ofType:@"plist"];
// Load the file content and read the data into arrays
self.dataArray = [NSArray arrayWithContentsOfFile:path];
//Sort the array by section
self.sortedArray = [self.dataArray sortedArrayUsingDescriptors:@[
[NSSortDescriptor sortDescriptorWithKey:@"Section" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"Title" ascending:YES]]];
self.temp = [[NSMutableDictionary alloc] init];
for (NSDictionary *dict in self.sortedArray) {
NSMutableArray *array = self.temp[dict[@"Section"]];
// No items with the same section key stored yet, so we need to initialize a new array.
if (array == NULL) {
array = [[NSMutableArray alloc] init];
}
// Store the title in the array.
[array addObject:dict[@"Title"]];
// Save the array as the value for the section key.
[self.temp setObject:array forKey:dict[@"Section"]];
}
self.policePowers = [self.temp copy]; // copy returns an immutable copy of temp.
//Section for sorting
self.sectionArray = [self.sortedArray valueForKeyPath:@"Section"];
NSLog(@"%@", self.sectionArray);
//Title
self.namesArray = [self.sortedArray valueForKeyPath:@"Title"];
//Offence
self.offenseArray = [self.sortedArray valueForKeyPath:@"Offence"];
//Points to Prove
self.ptpArray = [self.sortedArray valueForKeyPath:@"PTP"];
//Action
self.actionsArray = [self.sortedArray valueForKeyPath:@"Actions"];
//Notes
self.notesArray = [self.sortedArray valueForKeyPath:@"Notes"];
//Legislation
self.legislationArray = [self.sortedArray valueForKeyPath:@"Legislation"];
//PNLD
self.pnldArray = [self.sortedArray valueForKeyPath:@"PNLD"];
//Image
self.imageString = [self.sortedArray valueForKeyPath:@"image"];
标题标题部分
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.sectionArray objectAtIndex:section];
}
NumberOfSectionsTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.policePowers count];
}
行数部分
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *sectionrows = self.policePowers[self.sectionArray[section]];
return [sectionrows count];
}
更新
需要说明的是,如果两个项目具有相同的截面值,我想将它们自动分组到一个数组中,并将该数组映射到末尾的节值NSDictionary Dictionary dictionaryWithObjects:forKeys:基本上通过两个数组循环,并将当前索引的一个数组中的对象映射为同一索引的另一个数组中的对象的键。当你打电话的时候
self.policePowers = [NSDictionary dictionaryWithObjects:self.namesArray forKeys:self.sectionArray];
因此,它将self.sectionArray
中的项目映射为self.namesArray
中项目的键。查看plist文件,“Title”键路径(映射到self.namesArray
)的值为string,因此您的NSLog
结果有意义,因为self.namesArray
是字符串数组,而不是数组数组
我不知道你怎么会得到这样的结果
"Alcohol: Licensing/Drive unfit" = {
"Drive/attempt to drive/in charge whilst unfit or over",
"Drive/attempt to drive/in charge whilst unfit or over",
"Drive/attempt to drive/in charge whilst unfit or over",
}
那个阵列应该从哪里来
--编辑--
我不认为有一个简洁的方法来完成你想要的,所以它必须手动完成。我以前没有实际使用过[NSArray array with contents soffile:path]
,self.dataArray
是一个字典数组,每个字典项表示plist中的一个项目(项目44、项目45等)?如果是这样,您可以这样做:
NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
for (NSDictionary *dict in self.dataArray) {
NSMutableArray *array = temp[dict[@"Section"]];
// No items with the same section key stored yet, so we need to initialize a new array.
if (array == null) {
array = [[NSMutableArray alloc] init];
}
// Store the title in the array.
[array addObject:dict[@"Title"]];
// Save the array as the value for the section key.
[temp setObject:array forKey:dict[@"Section"]];
}
self.policePowers = [temp copy]; // copy returns an immutable copy of temp.
--重新编辑--
应用程序崩溃是因为self.policipowers
是NSDictionary
,而不是NSArray
;因此,它没有objectAtIndex:
方法。如果您试图获取章节标题,请尝试以下方法:
return [self.sectionArray objectAtIndex:section];
此外,如果您使用的是表视图,我基本上会按照您喜欢的方式对self.sectionArray
进行排序,然后每当需要填充每个节中的数据时,我都会使用self.policpowers[self.sectionArray[section]]
返回映射到该节标题的标题数组
--又一--
如果将其拆分为以下几行,将在哪里引发NSRangeException
?如果您NSLog
,结果是否与预期相符
NSString *title = self.sortedKeys[indexPath.section];
NSArray *array = self.policePowers[title];
NSString *value = array[indexPath.row];
我很困惑。表视图是一个数组,自然映射到NS(可变)数组,而不是NSDictionary。有一个字典数组更有意义,每个字典代表一行的数据。也许我的逻辑有缺陷。我该如何正确地去做呢?节是键。大概节是按某种顺序排列的(从表视图的角度来看,节是由一个数字索引的)。数组已排序,字典未排序。考虑一个区段数组,其中每个区段是一个字典,其中一个字典值是包含该区段的行的数组。行数组的每个元素都是一个字典,包含该行的所有信息。(这个方案有几个小的变化,但这是基本的方法。)好吧,也许我的逻辑有缺陷。我该如何正确地去做呢?节是我需要排序和填充uitableheader的关键。在一个数组中有三次“不适合或超过时驾驶/尝试驾驶/负责”的用例是什么?当某个字符串作为标题出现时,应该有一个吗?对不起,我来解释一下。我有50多个条目,每个条目都是一本词典。我需要将NSDIctionary中包含的每个项(如plist代码片段图像所示)的部分作为NSString进行排序。这就是我遇到的问题,也是我努力用标题节正确地动态填充uitableview的原因。换句话说,如果两个项目具有相同的节值,您希望将它们自动分组到一个数组中,将该数组映射到末尾的节值?是的,这正是我想要的。这可能吗?