Dynamically change the contents of a UITableView

I have an NSURL object that receives data from my site based on a variable entered by the user in the search bar.

I split this data into NSArray.

Once I have done this, I want to display the data in a UITableView.

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

i.e. Program loading, lack of data, so the UITableView is empty, then the user searches for one variable. Gets some data, and the content is loaded into a UITableView. Looking for a new variable, old data is removed from UITableView and new data is added?

I'm currently trying to do this with an interface constructor, but I fear that I might have to make my interface pragmatic so that I can destroy and re-create a UITableView, but I'm not sure.

Thanks for any help.

+4
source share
3 answers

Sure reloadData method in UITableView will do the trick

+7
source

Do not be afraid, subclassing UITableView is very simple. In xCode, just select a new file, select "Cocoa Touch Classes", "Objective-c class" and select "UITableView" from the "Subclass" drop-down list. xCode will add a subclass of UITableViewController complete with stubs to build.

I filled out a very simple example that retrieves table data from an array and is displayed from the delegate application. Since you suggested sending the reloadData message to a UITableView, refresh the displayed data.

As you probably learned, using InterfaceBuilder for this task is much more complicated than doing it programmatically.

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 
+3
source

This is a bit complicated, but my solution, which works very reliably, is the following: (suppose you have an array as many arrays, each of which represents a section and contains elements that are actually rows in the table).

This example is suitable for the situation when we download some data from the server (for example, JSON), and the result can be very different in the number of sections and / or lines.

void function, you can omit it

 -(void)addToPropertiesTable { //fullTableData is above mentioned two dimensional array int sectionsCount = _fullTableData.count; int count = 0; NSMutableArray *insertIndexPaths = [NSMutableArray array]; NSMutableArray *deleteIndexPaths = [NSMutableArray array]; for(int j = 0; j < sectionsCount; j++) { NSMutableArray *currentAdverts = [[NSMutableArray alloc] init]; [currentAdverts addObjectsFromArray:[_fullTableData objectAtIndex:j]]; count = [currentAdverts count]; int currentRowsInSection = [self.propertiesTable numberOfRowsInSection:j]; if(currentRowsInSection > 0) { //if any data in current tableView, lets get rid of them first for (int i = [self.propertiesTable numberOfRowsInSection:j] - 1; i >=0 ; i--) { [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:j]]; } } for (NSUInteger item = 0; item < count; item++) { [insertIndexPaths addObject:[NSIndexPath indexPathForRow:item inSection:j]]; } } [self.propertiesTable beginUpdates]; //we delete old rows - whether we need them or not [self.propertiesTable deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; if([self.propertiesTable numberOfSections]) { //if any sections, we remove them NSIndexSet *nsIndexSetToDelete = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[self.propertiesTable numberOfSections])]; [self.propertiesTable deleteSections:nsIndexSetToDelete withRowAnimation:UITableViewRowAnimationAutomatic]; } //here we have to set new sections, whether they have changed or not NSIndexSet *nsIndexSetToInsert = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,sectionsCount)]; [self.propertiesTable insertSections:nsIndexSetToInsert withRowAnimation:UITableViewRowAnimationAutomatic]; //finally we insert rows [self.propertiesTable insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade]; [self.propertiesTable endUpdates]; //now we see the change in UI [self.propertiesTable reloadData]; } 
0
source

All Articles