help with code please.

Discussion in 'iOS Programming' started by ashuk38, Apr 2, 2012.

  1. ashuk38 macrumors newbie

    Joined:
    Apr 2, 2012
    #1
    Hi guys

    Just joined today and looking for some expertise :)

    I have the following code which works ok but i need to push the selection into another page. Ie. when someone clicks on the cell say "K12" from the list i want it to display a page containing information relating to K12. I am very new at programming and is a mine field to say the least but I'm trying to learn in stages so please give me simple responses guys.

    The code i have so far in viewcontroller.m is:

    Code:
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	allItems = [[NSArray alloc] initWithObjects:@"K12", @"K14", @"K16", @"K18", @"K42", @"K43", @"K46", @"K53", @"K60", @"K68", @"K81", @"K83", nil];
        displayItems = [[NSMutableArray alloc] initWithArray:allItems];
        
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return  1;    
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [displayItems count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
        cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];
        return cell;
    }
    
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        if ([searchText length] == 0) {
            [displayItems removeAllObjects];
            [displayItems addObjectsFromArray:allItems];
        } else {
            //here
            [displayItems removeAllObjects];
            for (NSString * string in allItems) {
                NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
                if (r.location != NSNotFound) {
                    [displayItems addObject:string];
            }
        }
    }
    
        [tableView reloadData];
    }
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {
        [searchBar resignFirstResponder];
    }
        
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    @end
    
    any help would be very much appreciated..
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I think it might be easier if you head over to iTunes U, look for Stanford's free online classes on iOS programming, and start watching those...

    I'm not at my computer right now, but the method you need is something like "didSelectRowInTableView:withIndex:". I believe the delegate of your table view (likely the view controller) should be the one to implement that.
     
  3. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    Hmm, you're having 2 arrays, when you only need 1 :)
    Do this,

    Code:
    	displayItems = [[NSMutableArray alloc] initWithObjects:@"K12", @"K14", @"K16", @"K18", @"K42", @"K43", @"K46", @"K53", @"K60", @"K68", @"K81", @"K83", nil];
    
    Bam, you have the only array you need :)
    Also, to click it, like Art said above me, you need the "didSelectRowAtIndex", which is a Tableview delegate method (command click on UITableViewDelegate in your header file), and copy paste it.
    In that method, you need to PUSH a NEW CONTROLLER onto your navigationController with the details ;)

    When you can do something like that, then head back for more questions..

    Greetings, Noxx
     
  4. dejo, Apr 2, 2012
    Last edited: Apr 3, 2012

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Drill-down table-views is a pretty common need so you should be able to find plenty of sample code / tutorials out there. What resources have you been using thus far to learn the fundamentals? We ask that you be specific.

    P.S. A less-vague thread title would also help us to better gauge what your thread is about. And you can always edit the title as long as the thread is open.
     

Share This Page