创建NSDictionary以计算numberOfRowsInSection

时间:2014-05-08 16:15:42

标签: ios uitableview

我正在尝试为uiTableView正确显示部分和行。

我得到了一位撰稿人的大力帮助,并且非常接近解决我的问题。可以看到问题here。它距离正确,它只是需要排序的部分。

重复部分标题而不是仅显示一次。我不确定如何解决这个问题。

// 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"];

Plsit

titleForHeaderInSection

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.sectionArray objectAtIndex:section];
}

numberOfSectionsInTableView

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [self.policePowers count];
    }

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sectionrows = self.policePowers[self.sectionArray[section]];
    return [sectionrows count];
}

更新

要清楚,如果两个项具有相同的Section值,我想自动将它们分组到一个数组中,并将该数组映射到最后的Section值

1 个答案:

答案 0 :(得分:2)

NSDictionary dictionaryWithObjects:forKeys:基本上遍历两个数组,并将当前索引处的一个数组中的对象映射为同一索引处另一个数组中对象的键。当你打电话

self.policePowers = [NSDictionary dictionaryWithObjects:self.namesArray forKeys:self.sectionArray];
因此,

会将self.sectionArray中的项目映射为self.namesArray中项目的关键字。查看你的plist文件,"标题" keypath(映射到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 arrayWithContentsOfFile: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.policePowersNSDictionary,而不是NSArray;因此它没有objectAtIndex:方法。如果您尝试获取版块标题,请尝试以下方法:

return [self.sectionArray objectAtIndex:section];

此外,如果您正在使用表格视图,我基本上会按照您喜欢的方式排序self.sectionArray,然后每当我需要在每个部分填充数据时,我会使用{ {1}}返回映射到该节标题的标题数组。

- 再来一次 - 如果你把它分成以下几行,self.policePowers[self.sectionArray[section]]抛出在哪里?如果您NSRangeException,结果是否符合预期?

NSLog