2011-04-13 100 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