2011-04-13 77 views
0

我有一个首选项视图,显示不同的表视图取决于哪个分段控件被点击。NSMutableArray,pList,Tableview泥泞和崩溃

我硬编码了一些NSMutableArrays测试基本原则:

prefsIssuesList = [[NSMutableArray alloc] init]; 
[prefsIssuesList 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; 

然后重新装入currentArray的tableview中,加入UITableViewCellAccessoryCheckmark。 一切工作正常。

但现在我想要存储羯羊对号在plist文件中开启或关闭,并读取此回。

理想的情况下想这样

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

我有一个的plist至于工作了这一点

// 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 

但我真正想要做的是通过问题词典循环并获得键/值对这样

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

我假设我仍然可以将三个NSMutableArrays中的一个指定给currentArray,就像我在硬编码版本中所做的一样,并使用currentArray重新加载tableview。

然后修改这个代码来构建的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; 

但我的大脑已经融化,我已经花了六小时左右今天的Plist,NSArrays,NSMutableDisctionaries,standardUserDefaults收效甚微读了。

我已经设法在UINavigationViews中使用UITableViews,使用SegmentedControls,下载异步XML,但是现在我终于被卡住了,或者两者兼而有之。在什么应该是相当简单的键/值对。

任何关心给我一些白痴指针?

+0

PS,当我说的喜好观看我只是想说显示boolea一个UITableView的更好的方法n个选项(带有UITableViewCellAccessoryCheckmark),其中的XML位可显示在应用程序中的其他位置 – JulianB 2011-04-13 01:07:46

+0

对不起,但我想知道你确切的问题在哪里?你能够正确存储数据吗?检索有问题吗? – Ravin 2011-04-13 03:31:00

回答

0

键入它导致了另一个帖子,其中一个小的字,我需要让我回到正轨:)

使用键/值对的plist中,规定了小区的名称,还判定是否有人选择或不由用户。

的plist是基于这样

Root Dictionary 
     Services Dictionary 
        Peaches  String 1 
        Pumpkin  String 0 

的结构下面是如何抓住3个字典阵列从PLIST和所使用的键/值对来重新加载取决于一个的tableview上其中segmentControl被感动:

- (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; 
} 

然后样式表单元格

- (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; 
} 

的如果最后的条款似乎不起作用,我会将其作为一个新问题发布(除非有人快速评论!)

最后segmentControls分配不同的字典,以占位符阵列和重新加载的tableview

这花了我一个很漫长的一天搞清楚(作为noobie),所以我希望它可以帮助别人

-(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]; 

}

怒吼,如果有一个整洁或d