2011-10-02 61 views
2

我有一個由pList中的數據填充的部分UITableView。 pList的根是一個字典,這個字典的每個鍵被加載到一個數組中,它用來定義節標題(大概是有序的)。訂購從pList加載的UITableView部分

當前我的部分以隨機順序出現在表格中,我想按照它們出現在pList中的順序排列它們,或者至少按字母順序排列部分來僞造它們。

我知道字典沒有順序,這就是爲什麼輸出不能反映pList順序,但我不希望將根改爲數組,因爲它需要我重寫我的應用程序的塊,再加上Xcode 4似乎更喜歡使用字典作爲根。當然,如果更改pList結構是最好的方法,那麼我會繼續。

plist中樣品: 'TableViewController.m' 內

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>Section A</key> 
    <array> 
     <dict> 
      <key>Details</key> 
      <string>Details Here...</string> 
      <key>Image</key> 
      <string>X.png</string> 
      <key>Name</key> 
      <string>Person X</string> 
     </dict> 
     <dict> 
      <key>Details</key> 
      <string>Details Here...</string> 
      <key>Image</key> 
      <string>Y.png</string> 
      <key>Name</key> 
      <string>Person Y</string> 
     </dict> 
    </array> 
    <key>Section B</key> 
    <array> 
     <dict> 
      <key>Details</key> 
      <string>Details Here...</string> 
      <key>Image</key> 
      <string>Z.png</string> 
      <key>Name</key> 
      <string>Person Z</string> 
     </dict> 
    </array> 
</dict> 

代碼:

- (void)viewDidLoad { 

[super viewDidLoad]; 

NSString *path = [[NSBundle mainBundle] pathForResource:@"Team" ofType:@"plist"]; 
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
self.teamDictionary = tempDict; 
[tempDict release]; 

NSArray *tempArray = [[NSArray alloc] init]; 
self.teamArray = tempArray; 
[tempArray release]; 

self.teamArray = [self.teamDictionary allKeys]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

return [self.teamArray count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return [[self.teamDictionary valueForKey:[self.teamArray objectAtIndex:section]] count]; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

// this is here because I am altering the appearance of the header text 
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 44.0)] autorelease]; 

UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 

// some custom formatting stuff has been removed from here... 

headerLabel.text = [self.teamArray objectAtIndex:section]; 
[customView addSubview:headerLabel]; 
[headerLabel release]; 

return customView; 
} 

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

NSUInteger section = [indexPath section]; 
NSUInteger row = [indexPath row]; 
NSString *key = [teamArray objectAtIndex:section]; 
NSArray *teamMember = [teamDictionary objectForKey:key]; 

static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease]; 
} 

// Configure the cell... 

NSDictionary *name = [teamMember objectAtIndex:row]; 
cell.textLabel.text = [name objectForKey:@"Name"]; 
} 

回答

3

我能想到的兩個選項:

  1. 使用的陣列爲一體的根你的plist。數組中的每一項都是一個字典,有兩個鍵:名稱/標題和項目。名稱/標題將是一個字符串,項目將是一個數組。

  2. 保持你的plist原樣,命令鍵按字母順序:

    NSArray *sortedKeys = [[dict allkeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]]; 
    
+0

嘗試: 'self.sortedArray = [self.teamArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare :)];' ,然後用sortedArray替換對teamArray的引用,工作得很好! 謝謝! – MightyMeta