How to download dynamic data from a server to uitableview

I'm starting ios development, I need to load dynamic data from the server into tableview, if anyone knows the code, please share it.

Thanks in advance, Abe.

+6
source share
4 answers

Use the main data and NSFetchedResultsController , through the delegate methods of the tableview controller, fill in the table view from the NSFetchedResultsController .. It reflects the removal, addition and any kind of automatic database updates in the form of a table.

+5
source

Depending on the complexity level of your model and which caching policy you need, there are various ways to connect a table view to a remote data source. There would be many things here, but the best thing for you if you are a beginner is to take a look at some examples on the Internet.

There are several open source projects for network communications that offer good ways and good examples (including source code) for working with server-driven applications. I quote two that I prefer:

  • AFNetworking : there are sample sources with this library and a UITableViewController that may suit your needs
  • RestKit : this will be useful if you want to map and save the data returned by the server to a local model. This may require some more training.

But remind you that you still need to know the basics of UITableView and the corresponding protocols: UITableViewDelegate and UITableViewDataSource . The documentation is fine, but you can even take a look at the WWDC 2011 podcast on table views.

In addition, if you need persistent data, you should start looking for Core Data and NSFetchedResultControllers , as both Illis and Bogdan say, but things will get a little more complicated.

+3
source

Actually, NSFetchedResultsController is a little hard to understand.

You should try to process the UITableView data yourself. See the UITableViewDelegate protocol

Here are a few steps for you:

1) Create a class that inherits UITableViewDelegate and UITableViewDataSource

@interface YourTableViewController: UITableViewController<UITableViewDelegate,UITableViewDataSource> 

2) Create an array in which your data will be stored.

 @property (nonatomic,retain) NSMutableArray *data; 

3) Perform the following methods:

  - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DataObject *d=[data objectAtIndex:indexPath.row]; // selected data, now you can handle it } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return data.count; } - (UITableViewCell *)tableView:(UITableView *)mtableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString * ident=@ "CatalogCell"; UITableViewCell *cell=(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ident]; if (cell==nil) { cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident] autorelease]; } DataObject *d=[data objectAtIndex:indexPath.row]; [cell.textLabel setText:d.someField]; } 

4) Select a method ( NSURLConnection , ASIHTTPRequest (sorry, only 2 hyperlinks are allowed for me) or others) to receive data from the server

 -(void) parseData:(NSString *) d { NSArray * parsedData=[self someMethodToParseData:d]; [data setArray:parsedData]; [tableView reloadData]; } 
+2
source

You can use the ASIHTTPRequest library to receive data from your server. You can find information about this at http://allseeing-i.com/ASIHTTPRequest/

You can use http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-use-tableview-in-iphone/ this link for more information about table view.

In the link tutorial, you should request your data in viewDidload mode.

 - (void)viewDidLoad { // Request your data on this line. /*NSArray *array = [[NSArray alloc] initWithObjects:@"Sleepy",@"Sneezy",@"Bashful",@"Happy",@"Doc", @"Grmpy",@"Dopey",@"Thorin",@"Dorin",@"Nori", @"Ori",@"Balin",@"Dwalin",@"Fili",@"Kili",@"Oin", @"Gloin",@"Bifur",@"Bofur",@"Bombur",nil ];*/ self.listData = array; [array release]; [super viewDidLoad]; } 

All this is just use. You should experience more examples of using a more complex situation. I will add additional code for the tutorial to retrieve data from the server and display it in a table.

0
source

Source: https://habr.com/ru/post/921835/


All Articles