开发者

NSMutableArray, pList, Tableview muddle and meltdown

I have a preferences view which shows a different table view depending on which Segmented Control is clicked.

I hard coded some NSMutableArrays to test basic principles:

prefsIssuesList = [[NSMutableArray alloc] init];
[pr开发者_Go百科efsIssuesList addObject:@"Governance"];
[prefsIssuesList addObject:@"Innovation and technology"];
...etc

prefsIndustriesList = [[NSMutableArray alloc] init];
[prefsIndustriesList addObject:@"Aerospace and defence"];
... etc

prefsServicesList = [[NSMutableArray alloc] init];
[prefsServicesList addObject:@"Audit and assurance"];
...etc

currentArray = [[NSMutableArray alloc] init];
currentArray = self.prefsIssuesList;

Then reload the tableview with currentArray, adding a UITableViewCellAccessoryCheckmark. Everything works fine.

But now I want to store wether the checkmark is on or off in a pList file, and read this back in.

Ideally want to a plist like this

Root    Dictionary
    Issues  Dictionary
        Governance         Number   1
        Innovation and technology  Number   0
        etc

I've got as far as working this out

// Designate plist file
NSString *path = [[NSBundle mainBundle] pathForResource: @"issues" ofType:@"plist"];
// Load the file into a Dictionary
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.allNames= dict;
[dict release];

NSLog(@"Dict is %@", allNames); // All the data in the pList file

NSMutableArray *issueSection = [allNames objectForKey:@"Issues"];
NSLog(@"Issues is %@", issueSection); // The data is the Issues Section

NSString *issueVal = [issueSection objectForKey:@"Governance"];
NSLog(@"Governance is %@", issueVal); //The value of the Governance key

But what I really want to do is loop through the Issues Dictionary and get the key/value pairs so

key   =  cell.textLabel.text
value =  UITableViewCellAccessoryCheckmark / UITableViewCellAccessoryNone 
         depending wether it's 1 or 0

I'm assuming that I can still assign one of the three NSMutableArrays to currentArray as I did in the hardcoded version, and use currentArray to reload the tableview.

Then amend this code to build the tableview

NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];

NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];

static NSString *CellIdentifier = @"Cell";

//UITableViewCell *cell = [self.prefsTableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
UITableViewCell *cell = [self.prefsTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
    cell=[[[UITableViewCell alloc] 
           initWithFrame:CGRectZero
           reuseIdentifier: CellIdentifier] autorelease];
}

cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;

But my brain has melted, I've spent about six hours today reading up on pLists, NSArrays, NSMutableDisctionaries, standardUserDefaults to little avail.

I've managed to UITableViews inside UINavigationViews, use SegmentedControls, download asynchronous XML, but now I'm finally stuck, or fried, or both. Over what should be fairly simple key/value pairs.

Anyone care to give me some idiot pointers?


Typing it out led to another post with that one little word I needed to get me back on track :)

Use key/value pairs in a pList to stipulate the name of the cell and wether it was selected or not by the user.

plist is based on a structure like this

Root    Dictionary
        Services    Dictionary
                    Peaches       String    1
                    Pumpkin       String    0

Here's how I grabbed three Dictionary arrays from a pList and used the key/value pairs to reload a tableview depending on which segmentControl was touched:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Designate plist file
    NSString *path = [[NSBundle mainBundle] pathForResource: @"issues" ofType:@"plist"];
    // Load the file into a Dictionary
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.allNames= dict;
    [dict release];

    // Create the Named Dictionaries from Dictionary in pLIst
    NSMutableDictionary *allIssues = [self.allNames objectForKey:@"Issues"];
    self.prefsIssuesList = allIssues;
    [allIssues release];

    NSMutableDictionary *allIndustries = [self.allNames objectForKey:@"Industries"];
    self.prefsIndustriesList = allIndustries;
    [allIndustries release];

    NSMutableDictionary *allServices = [self.allNames objectForKey:@"Services"];
    self.prefsServicesList = allServices;
    [allServices release];

    // Assign the current Dictionary to out placeholder Dictionary 
    currentDict = [[NSMutableDictionary alloc] init];
    currentDict = self.prefsIssuesList;
}

Then styling the table cells

- (UITableViewCell *)tableView:(UITableView *)prefsTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];

    NSArray *keysArray = [self.currentDict allKeys];
    NSString *theKey = [keysArray objectAtIndex:row];
    NSString *theValue = [self.currentDict objectForKey: [keysArray objectAtIndex:row]];   

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.prefsTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell=[[[UITableViewCell alloc] 
               initWithFrame:CGRectZero
               reuseIdentifier: CellIdentifier] autorelease];
    }

    cell.textLabel.text = theKey;

    if (theValue == @"0") { 
        cell.accessoryType = UITableViewCellAccessoryNone; 
    }else { 
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    return cell;
}

The if clause at the end doesn't seem to be working, I'll post that as a new question (unless anyone comments quickly!)

Finally the segmentControls assign the different dictionaries to the placeholder array and reload the tableview

This took me a very long day to figure out (as a noobie) so I hope it helps someone

-(IBAction) segmentedControlIndexChanged{
switch (self.segmentedControl.selectedSegmentIndex) {
    case 0:
        //currentArray = self.prefsIssuesList;
        currentDict = self.prefsIssuesList;
        break;
    case 1:
        //currentArray = self.prefsIndustriesList;
        currentDict = self.prefsIndustriesList;
        break;
    case 2:
        //currentArray = self.prefsServicesList;
        currentDict = self.prefsServicesList;
        break;

    default:
        //currentArray = self.prefsIssuesList;
        currentDict = self.prefsIssuesList;
        break;
}
[prefsTableView reloadData];

}

Shout if there's a neater or better way of d

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜