質問

I am trying to display sections and rows correctly for my uiTableView.

I have had great help from one contributor and am fairly close to fixing my issue. The Issue can be seen here. Its not far off being right, its just the sections that need to be sorted.

It is repeating the section titles instead of only showing it once. Im not sure exactly how to fix this.

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

Update

To be clear, if two items have the same Section value, I want to automatically group them into an array and have that array mapped to the Section value at the end

役に立ちましたか?

解決

NSDictionary dictionaryWithObjects:forKeys: basically loops through two arrays and maps the object in one array at the current index as the key for the object in the other array at the same index. When you're calling

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

it therefore maps the items in self.sectionArray as the keys for the items in self.namesArray. Looking at your plist file, the "Title" keypath (which is mapped to self.namesArray) has a value of string, so your NSLog results make sense, as self.namesArray is an array of strings, not an array of arrays.

I'm not sure how you were supposed to get a result like

"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",
 }

Where is that array supposed to come from?

-- EDIT --

I don't think there's a concise way to accomplish what you want, so it'd have to be done manually. I haven't actually used [NSArray arrayWithContentsOfFile:path] before, so is self.dataArray an array of dictionaries with each item representing one of the items in the plist (Item 44, Item 45, etc)? If so, you could do something like this:

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.

-- EDIT AGAIN -- The app crashes because self.policePowers is an NSDictionary, not an NSArray; thus it doesn't have an objectAtIndex: method. If you're trying to get the section title, try this instead:

return [self.sectionArray objectAtIndex:section];

Furthermore, if you're working with a table view, I'd basically have self.sectionArray sorted whichever way you like, then whenever I needed to populate data in each section, I would use self.policePowers[self.sectionArray[section]] to return the array of titles mapped to that section title.

-- YET ANOTHER -- If you break it up into the following lines, where is the NSRangeException thrown? If you NSLog, do the results match what you expect?

NSString *title = self.sortedKeys[indexPath.section];
NSArray *array = self.policePowers[title];
NSString *value = array[indexPath.row];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top