Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Changing UITableView's Contents

I have an NSURL object which gets data from my site, based on a variable entered by the user into the search bar.

I split this data into an NSArray.

Once I have done that I wish to display the data in a UITableView.

My question is this. Is it possible to load the data into a UITableView dynamically?

i.e. Program loads, no data so UITableView is empty, then the user searches for one variable. Gets some data and the contents is loaded into the UITableView. Searches for a new variable, old data is cleared from UITableView and the new data is added?

I'm currently trying to do this using interface builder, but fear that I may have to make my interface pragmatically, so that I could destroy and re-create the UITableView, but i'm, not sure.

Thanks for any help.

like image 433
JonB Avatar asked Jul 15 '09 13:07

JonB


2 Answers

Sure the method reloadData on UITableView will do the trick

like image 191
Daniel Avatar answered Oct 03 '22 09:10

Daniel


Fear not, subclassing UITableView is very easy. In xCode simply choose new file, choose "Cocoa Touch Classes", "Objective-c class" and in the "Subclass of" dropdown pick "UITableView". xCode will add a UITableViewController subclass complete with stubs to build on.

I filled in a very simple example that draws the table data from an array and is displayed from the Application Delegate. As you suggested sending a reloadData message to the UITableView will refresh the displayed data.

As you probably found out, using InterfaceBuilder for this job is a lot harder than doing it programatically.

Cheers, niels

//
//  MyTableViewController.m
//  TableView
//
//  Created by Niels Castle on 7/15/09.
//  Copyright 2009 Castle Andersen ApS. All rights reserved.
//

#import "MyTableViewController.h"


@implementation MyTableViewController

// Initializer do custom initialisation here
- (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {

      // This is the source of my data. The simplest source possible,
      // an NSMutableArray, of name. This would be the data from your web site
       array = [[NSMutableArray alloc]
        initWithObjects:@"Niels", @"Camilla", @"William", nil];
    }
    return self;
}


// How many section do we want in our table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view
// Simply the number of elements in our array of names
- (NSInteger)tableView:(UITableView *)tableView
    numberOfRowsInSection:(NSInteger)section {
    return [array count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   // Reuse cells 
   static NSString *id = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
            initWithStyle: UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier] autorelease];
    }

   // Simplest possible cell - displaying a name from our name array
   [[cell textLabel] setText: [array objectAtIndex:[indexPath row]]];

    return cell;
}

- (void)dealloc {
   [super dealloc];
   [array release];
}

@end


//
//  TableViewAppDelegate.m
//  TableView
//
//  Created by Niels Castle on 7/15/09.
//  Copyright Castle Andersen ApS 2009. All rights reserved.
//

#import "TableViewAppDelegate.h"
#import "MyTableViewController.h"

@implementation TableViewAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

   MyTableViewController *twc = [[MyTableViewController alloc]
      initWithStyle: UITableViewStylePlain];
   [window addSubview: [twc view]];

    [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end
like image 22
Niels Castle Avatar answered Oct 03 '22 10:10

Niels Castle