Domanda

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!

È stato utile?

Soluzione

Don't use 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:).

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

self.tableContents = [NSDictionary dictionaryWithObjects:values forKeys:keys];
self.sortedKeys = keys;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top