2014-04-06 62 views
0

我正在使用下面的示例代碼來具有「分組表格視圖」中的部分內容。顯示「分組表格視圖」中的內容很好。 但問題是,它會根據字母順序對節內容進行排序,因此節標題內容的順序不會按預期方式顯示。例如:我希望「關於」在tableview的部分末尾顯示,但在這裏它總是首先顯示(因爲按字母順序排序)。我如何根據下面的代碼顯示部分內容而無需按字母順序排序。請指教!iOS:如何在分組表格視圖中顯示節內容?

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     NSArray *arrTemp4 = [[NSArray alloc]initWithObjects:@"Quick Logon",@"Stay Logged On", nil]; 
     NSArray *arrTemp3 = [[NSArray alloc]initWithObjects:@"Notifications",@"Text Messaging",@"Family Members",@"Social Media",nil]; 
     NSArray *arrTemp2 = [[NSArray alloc]initWithObjects:@"Payment Accounts",nil]; 
     NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:@"Phone Nickname",@"Version",nil]; 

     NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys: arrTemp1,@"About", arrTemp2,@"Payment Preferences", arrTemp3,@"Profile & Preferences", arrTemp4,@"Security ", nil]; 

     self.tableContents = temp; 

     NSLog(@"table %@",self.tableContents); 
     NSLog(@"table with Keys %@",[self.tableContents allKeys]); 

self.sortedKeys = [[self.tableContents allKeys] sortedArrayUsingSelector:@selector(compare:)]; 


     NSLog(@"sorted %@",self.sortedKeys); 

    } 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return [self.sortedKeys count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [self.sortedKeys objectAtIndex:section]; 
} 

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { 
    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:section]]; 
    return [listData count]; 
} 


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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]]; 

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 

    if(cell == nil) { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]; 
     //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    } 
    cell.accessoryType = indexPath.section > 0 ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone; 

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [listData objectAtIndex:row]; 

    NSLog(@"indexPath.section %d",indexPath.section); 


    if (indexPath.section==0) 
    { 
     // cell.imageView.image = [UIImage imageNamed:@"icon.png"]; 
    } 
    //cell.switch.hidden = indexPath.section > 0; 

    return cell; 
} 

在此先感謝!

回答

0

請勿使用sortedArrayUsingSelector:。相反,請按照需要的順序顯式創建鍵陣列。然後,使用該鍵陣列創建tableContents,並創建另一個陣列以容納所有內容數組(使用dictionaryWithObjects:forKeys:)。

NSArray *keys = @[ @"About", @"Payment Preferences", @"Profile & Preferences", @"Security" ]; 
NSArray *values = @[ arrTemp1, arrTemp2, arrTemp3, arrTemp4 ]; 

self.tableContents = [NSDictionary dictionaryWithObjects:values forKeys:keys]; 
self.sortedKeys = keys; 
+0

我已經粘貼了我所有的代碼。 – Stella

+0

有什麼你不明白我的回答? – Wain

+0

我會嘗試。謝謝 – Stella

相關問題