简体   繁体   English

iOS:如何在分组表视图中显示节内容?

[英]iOS: How can i display the section content in Grouped Table View?

I am using the following sample code to have the section contents in "Grouped Table View". 我正在使用以下示例代码在“分组表视图”中显示节内容。 Showing the contents in "Grouped Table View" is fine. 在“分组表视图”中显示内容可以。 But, the issue is, it sorts the section content based on alphabetical order, so the order of section header content is not displaying as expected. 但是,问题是,它根据字母顺序对节内容进行排序,因此节标题内容的顺序未按预期显示。 For example: I want "About" to be shown at the end of section in tableview, but here it is always shows first (because of alphabetical sort does there). 例如:我希望在表视图的部分末尾显示“关于”,但是这里总是始终显示(因为这里按字母顺序排序)。 How can i display the section content based on the below code without alphabetical sorting. 如何在不按字母顺序排序的情况下基于以下代码显示部分内容。 Please advise! 请指教!

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

Thanks in Advance! 提前致谢!

Don't use sortedArrayUsingSelector: . 不要使用sortedArrayUsingSelector: Instead, explicitly create the key array in the order that you want. 而是,按所需顺序显式创建键数组。 Then, create tableContents using that key array and another array created to hold all of your content arrays (using dictionaryWithObjects:forKeys: ). 然后,使用该键数组和另一个创建的数组创建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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM