UITableView - setting up a table view with Hpple data

I get some data from an XML file on the Internet using hpple. In my XML file, I have a bunch of different sections from which I get data (about 20).

Here is the code I use to get the data

- (void)getMenuItems:(NSString*)url{ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { //NSLog(@"response == %@", response); [self getDeli:data]; } - (void)getDeli:(NSData*)deliData { TFHpple *Parser = [TFHpple hppleWithHTMLData:deliData]; // 3 NSString *XpathQueryString = self.deliString; NSArray *Nodes = [Parser searchWithXPathQuery:XpathQueryString]; // 4 NSMutableArray *newNodes = [[NSMutableArray alloc] initWithCapacity:0]; for (TFHppleElement *element in Nodes) { // 5 Items *item = [[Items alloc] init]; [newNodes addObject:item]; // 6 item.title = [[element firstChild] content]; item.title = [[[element firstChild] content]stringByReplacingOccurrencesOfString:@"\n" withString:@""]; // 7 item.url = [element objectForKey:@"href"]; } // 8 _deli = newNodes; [self.tableView reloadData]; 

}

Everything works, and I get the data, the problem is that not all sections have data, and setting the section headers and setting the data in CellForRowAtIndexPath is a problem, because I change it to return the same with the number of sections.

So, I'm looking to find out how many sections will be returned, and set this to the number of sections in the table view, and then set the section headers to the header in the section headers of the xml file, etc.

So, I do not just set the height of the header and row to 0 if the section or cell is empty.

I hope this makes sense ...

Here is most of the xml file

 <day name="monday"> <meal name="DINNER"> <counter name="Chefs Choice"> <dish> <name>Vegetable Samosa with Yogurt Sauce</name> </dish> <dish> <name>Tomato Red Pepper Chutney</name> </dish> <dish> <name>Curried Rice & Lentils</name> </dish> </counter> <counter name="EntrΓ©e"> <dish> <name>London Broil</name> </dish> <dish> <name>Oven Roast Rosemary Red Potatoes</name> </dish> <dish> <name>Glazed Fresh Carrots</name> </dish> <dish> <name>Salad Bar</name> </dish> <dish> <name>Cheddar Cheese & Bacon Potato Salad</name> </dish> </counter> <counter name="Grill"> <dish> <name>Hamburger</name> </dish> <dish> <name>Classic Cheeseburger on a Toasted Bun</name> </dish> <dish> <name>Chicken Sandwich</name> </dish> <dish> <name>French Fries</name> </dish> </counter> <counter name="International"> <dish> <name>Shell Pasta</name> </dish> <dish> <name>Spaghetti Sauce with Tomato Bits</name> </dish> <dish> <name>Alfredo Sauce</name> </dish> </counter> <counter name="Pizza"> <dish> <name>Cheese Pizza</name> </dish> <dish> <name>Pepperoni Pizza</name> </dish> <dish> <name>Antipasto Pizza Slice</name> </dish> </counter> <counter name="Soup"> <dish> <name>Tomato Soup Florentine</name> </dish> </counter> <counter name="Vegetable"> <dish> <name>Eggplant Parmesan Casserole</name> </dish> </counter> </meal> </day> 

Thanks for the help in advance.

+6
source share
2 answers

You need to analyze the XML structure in the corresponding data model object so that you can access the data through a hierarchy.

This is a data model class that can work -

 #import <Foundation/Foundation.h> typedef NS_ENUM(NSInteger, DeliDay) { DeliDayMonday=0, DeliDayTuesday=1, DeliDayWednesday=2, DeliDayThursday=3, DeliDayFriday=4, DeliDaySaturday=5, DeliDaySunday=6, DeliDayUnknown=-1 }; @interface DeliData : NSObject @property (strong,nonatomic,readonly) NSArray *days; @property (strong,nonatomic,readonly) NSArray *meals; @property (strong,nonatomic,readonly) NSArray *counters; -(id)init:(NSData *)menuData; -(NSArray *) itemsForMeal:(NSString *)meal onDay:(DeliDay)day atCounter:(NSString *)counter ; -(NSArray *) mealsForDay:(DeliDay)day; -(NSArray *) countersForMeal:(NSString *)meal onDay:(DeliDay)day; +(DeliDay)deliDayForString:(NSString *)dayString; +(NSString *)stringForDeliDay:(DeliDay)day; @end 

The parse method should work through tags and build data structures -

 -(void)parseXML:(NSData *)menuData { TFHpple *Parser = [TFHpple hppleWithXMLData:menuData]; NSString *XpathQueryString = @"//day"; NSArray *Nodes = [Parser searchWithXPathQuery:XpathQueryString]; for (TFHppleElement *element in Nodes) { NSString *dayString=element.attributes[@"name"]; DeliDay day=[DeliData deliDayForString:dayString]; if (day != DeliDayUnknown) { NSMutableDictionary *dayDict=(NSMutableDictionary *)self.menuData[day]; NSArray *mealsArray=[element childrenWithTagName:@"meal"]; for (TFHppleElement *mealElement in mealsArray) { NSString *mealName=mealElement.attributes[@"name"]; NSMutableDictionary *counterDict=(NSMutableDictionary *)dayDict[mealName]; if (counterDict == nil) { counterDict=[NSMutableDictionary new]; dayDict[mealName]=counterDict; } NSArray *countersArray=[mealElement childrenWithTagName:@"counter"]; for (TFHppleElement *counterElement in countersArray) { NSString *counterName=counterElement.attributes[@"name"]; NSMutableArray *itemsArray=(NSMutableArray *)counterDict[counterName]; if (itemsArray == nil) { itemsArray=[NSMutableArray new]; counterDict[counterName]=itemsArray; } NSArray *dishArray=[counterElement childrenWithTagName:@"dish"]; for (TFHppleElement *dishElement in dishArray) { Item *newItem=[Item new]; TFHppleElement *dishNameElement=[dishElement firstChildWithTagName:@"name"]; NSString *text=[[dishNameElement firstTextChild].content stringByReplacingOccurrencesOfString:@"\n" withString:@""]; newItem.title=text; TFHppleElement *dishUrlElement=[dishElement firstChildWithTagName:@"url"]; text=[[dishUrlElement firstTextChild].content stringByReplacingOccurrencesOfString:@"\n" withString:@""]; newItem.url=text; [itemsArray addObject:newItem]; } } } } else { NSLog(@"Invalid day name %@",dayString); } } } 

init method -

 -(id)init:(NSData *)menuData { if (self=[super init]) { [self populateMenuArray]; [self parseXML:menuData]; } return self; } 

where menuData is the NSData returned from your URL. You must create a model using

  self.deliData=[[DeliData alloc]init:menuData]; 

The whole class is available here - https://gist.github.com/paulw11/da11ab3c7688c6deb1d7

+2
source

Use this

 -(id)init:(NSData *)menuData { if (self=[super init]) { [self populateMenuArray]; [self parseXML:menuData]; } return self; 

}

0
source

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


All Articles