Friday, 24 February 2012

sectioned TableView + iphone

http://www.mobisoftinfotech.com/blog/iphone/iphone-uitableview-tutorial-grouped-table/
/*<?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>Photo</key>
 <string>mobile.png</string>
 <key>Mobile Phones</key>
 <array>
 <string>GSM Phones</string>
 <string>Dual Sim Phones</string>
 <string>CDMA Phones</string>
 </array>
 <key>Smart Phones</key>
 <array>
 <string>IOS Phones</string>
 <string>Android OS Phones</string>
 <string>Blackberry Phones</string>
 <string>Symbian OS Phones</string>
 <string>Tablet Phones</string>
 <string>Windows OS Phones</string>
 <string>Bada OS Phones</string>
 </array>

 <key>Mobile Accessories</key>
 <array>
 <string>Screen Protectors</string>
 <string>Memory Card</string>
 <string>HeadPhones</string>
 <string>CaseMate</string>       
 <string>Bluetooth Devices</string>       
 </array>

 <key>Landline Phones</key>
 <array>
 <string>Landline Phones</string>
 </array>

 </dict>
 </plist>
 */

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden=NO;
        self.title=@"Sub Category";
    appDel=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSLog(@"indexValue %@",indexValue);
    NSString *getPlist=[[NSBundle mainBundle] pathForResource:indexValue ofType:@"plist"];
    NSLog(@"%@",getPlist);
    plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:getPlist];
    NSLog(@"%@",plistDict);
   
   
    NSString *getPhoto=[plistDict valueForKey:@"Photo"];
    UIImageView *bgImgView=[[UIImageView alloc] initWithFrame:CGRectMake(subCatTableView.frame.origin.x, subCatTableView.frame.origin.y, subCatTableView.frame.size.width, subCatTableView.frame.size.height)];
    bgImgView.image=[UIImage imageNamed:getPhoto];
    bgImgView.alpha=0.5;
    subCatTableView.backgroundView=bgImgView;
    [plistDict removeObjectForKey:@"Photo"];
   
   
   
    sectionArray=[[NSMutableArray alloc] init];

    sectionArray =[[[plistDict allKeys]sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
   
    NSLog(@"%@",sectionArray);
    subCatArray=[[NSMutableArray alloc] init];
   

   
}
#pragma mark Table Methods

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

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

- (NSInteger)tableView:(UITableView *)table
 numberOfRowsInSection:(NSInteger)section {
    NSArray *listData =[plistDict objectForKey:
                        [sectionArray objectAtIndex:section]];
    return [listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
   
    NSArray *listData =[plistDict objectForKey:
                        [sectionArray objectAtIndex:[indexPath section]]];
   
    UITableViewCell * cell = [tableView
                              dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
   
    if(cell == nil) {
       
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier:SimpleTableIdentifier] autorelease];
         cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        /*cell = [[[UITableViewCell alloc]
         initWithStyle:UITableViewCellStyleSubtitle
         reuseIdentifier:SimpleTableIdentifier] autorelease];
         */
    }
    cell.selectionStyle=UITableViewCellSelectionStyleGray;
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
   
    return cell;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *listData =[plistDict objectForKey:
                        [sectionArray objectAtIndex:[indexPath section]]];
    NSUInteger row = [indexPath row];
    NSString *rowValue = [listData objectAtIndex:row];
   
    NSString *message = [[NSString alloc] initWithFormat:rowValue];
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"You selected"
                          message:message delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    [message release];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
}

No comments:

Post a Comment