table view problem

Discussion in 'iOS Programming' started by ghousemd, Jul 9, 2011.

  1. ghousemd, Jul 9, 2011
    Last edited by a moderator: Jul 9, 2011

    ghousemd macrumors newbie

    Joined:
    Jun 25, 2011
    #1
    Hi, everybody in this app i am creating a table view with different rows and sections.it is displying properly. but problem is when i tried to retrieve a row which i am selected and try to display it .but it is displaying first row of last section.. i am using below code to retrieve row i selected.please tell me what i am doing wrong
    Code:
    controller.k=[nameSection objectAtIndex: indexPath.row];
    
    #import "datas.h"
    #import "NSDictionary-MutableDeepCopy.h"
    #import "De.h"
    
    @implementation datas
    
    - (id)initWithStyle :   (UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    @synthesize names; 
    @synthesize keys;
    @synthesize table;
    @synthesize search; 
    @synthesize allNames,h,nameSection;
    
    - (void)resetSearch {
        NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy]; 
        self.names = allNamesCopy; 
        [allNamesCopy release]; 
        NSMutableArray *keyArray = [[NSMutableArray alloc] init];
        [keyArray addObjectsFromArray:[[self.allNames allKeys] sortedArrayUsingSelector:   @selector(compare:)]];
        self.keys = keyArray; 
        [keyArray release];
    }
    - (void)handleSearchForTerm:       (NSString *)searchTerm {
        NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init]; 
        [self resetSearch];
        for (NSString *key in self.keys) 
        { 
            NSMutableArray *array = [names valueForKey:key]; 
            NSMutableArray *toRemove = [[NSMutableArray alloc] init]; 
            for (NSString *name in array) 
                if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound) [toRemove addObject:name];
            if ([array count] == [toRemove count]) 
                [sectionsToRemove addObject:key];
            [array removeObjectsInArray:toRemove]; 
            [toRemove release];
        }
        [self.keys removeObjectsInArray:sectionsToRemove];
        [sectionsToRemove release];
        [table reloadData];
    }
    
    - (void)dealloc
    {
        [table release];
        [search release]; 
        [allNames release]; 
        [keys release]; 
        [names release];
        [super dealloc];
        
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    -(void)viewDidLoad
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Property List1" ofType:@"plist"];
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
        self.allNames = dict; 
        [dict release];
        [self resetSearch]; 
        [table reloadData]; 
        [table setContentOffset:CGPointMake(0.0, 44.0) animated:NO];
        [super viewDidLoad];
        }
    
    
    - (void)viewDidUnload
    {
        self.names = nil; 
        self.keys = nil;
        self.table = nil;
        self.search = nil;
        self.allNames = nil;
        
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    #pragma mark - 
    #pragma mark Table View Data Source Methods
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return ([keys count] > 0) ? [keys count] : 1;
    } 
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
        
        if ([keys count] == 0) return 0;
        NSString *key = [keys objectAtIndex:section]; 
        nameSection = [names objectForKey:key];
        
        return [nameSection count];
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        
        NSInteger section = [indexPath section]; 
        NSInteger row = [indexPath row];
        NSString *key = [keys objectAtIndex:section]; 
        nameSection = [names objectForKey:key];
        static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SectionsTableIdentifier];
        if (cell == nil) 
        { 
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease];
        }
        cell.textLabel.text = [nameSection objectAtIndex:row]; 
        // cell.textLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"name"];
        //NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
    	//cell.text = [dictionary objectForKey:@"Title"];
        h=[nameSection objectAtIndex:row] ;
        //NSLog(@"%@",h);
        
        return cell;
    }
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    { 
        if ([keys count] == 0)
            return nil;
        NSString *key = [keys objectAtIndex:section]; return key;
        
    }
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
    { 
        if (isSearching) return nil;
        return keys;
    }
    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        isSearching = NO; 
        search.text = @""; 
        [tableView reloadData];
        [search resignFirstResponder]; 
        return indexPath;
    }
    
    - (void)searchBarSearchButtonClicked:       (UISearchBar *)searchBar 
    {
        NSString *searchTerm = [searchBar text]; 
        [self handleSearchForTerm:searchTerm];
        
    }
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:      (NSString *)searchTerm 
    { 
        if ([searchTerm length] == 0) 
        {
            [self resetSearch]; [table reloadData]; 
            return;
        } 
        [self handleSearchForTerm:searchTerm];
    }
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
    { 
        isSearching = NO;
        search.text = @""; 
        [self resetSearch];
        [table reloadData];
        [searchBar resignFirstResponder];
    }
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
    { 
        isSearching = YES; [table reloadData];
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
        De *controller = [[De alloc] initWithNibName:@"De" bundle:nil];
        /
        controller.k=[nameSection objectAtIndex:indexPath.row];
        //NSLog(@"%@",controller.k);
        
        [self.navigationController pushViewController:controller animated:YES];
        [controller release];
    }
    
    @end
    
     
  2. Shawnpk, Jul 9, 2011
    Last edited by a moderator: Jul 9, 2011

    Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #2
    Sorry. Are you saying that every cell pushes the same view?
     
  3. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #3
    Where do you set the value of nameSection, and where do you update it to match the currently selected cell (i.e., the one specified by indexPath)?
     
  4. ghousemd thread starter macrumors newbie

    Joined:
    Jun 25, 2011
    #4


    thanks because of ur suggestion i came to know about the problem and it is solved
     

Share This Page