how to pass array data to another method
I am currently connecting to a database that then gets a bunch of xml that I parse with NSXMLParser (as shown below) Once this is done I would like to pass the array data to another method to sort it and then seperate the array into sections (this is so my ui开发者_StackOverflow社区tableview Index will work). And finally displayed in my tableview.
My Main question is how (where in the code) can I pass my data array over to the method I have created.
#pragma mark - Parsing lifecycle
- (void)startTheParsingProcess:(NSData *)parserData
{
[myDataArray release]; // clears array for next time it is used.
myDataArray = [[NSMutableArray alloc] init]; //initalizes the array, this is a must have!!!! :)
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //incoming parserDatapassed to NSXMLParser delegate which starts parsing process
[parser setDelegate:self];
[parser parse]; //Starts the event-driven parsing operation.
[parser release];
//[self.tableView reloadData]; // this reloads the data in the tabel view once the nsmutablearray has been filled with values
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqual:@"item"]) {
// NSLog(@"Found title!");
itemString = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[itemString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqual:@"item"]) {
//NSLog(@"ended title: %@", itemString);
[myDataArray addObject:itemString]; //this is where i pass the values over to my array.
//TODO: Test release on memory consumption etc
[itemString release];
itemString = nil;
}
}
//method to sort array and split for use with uitableview Index
- (IBAction)startSortingTheArray:(NSMutableArray *)arrayData
{
}
Implement didEndDocument delegate method for your NSXMLParser, if your - (IBAction)startSortingTheArray:(NSMutableArray *)arrayData return type is non IBAction, lets say its return type is void the following should work fine. I don't know what happens when you pass arrayData to IBAction return type method.
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[self startSortingTheArray:myDataArray];
}
Would this work?
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self startSortingTheArray:myDataArray];
}
Not quite sure of the question - you want to pass the array to another method, your startSortingTheArray:
method appears to be defined correctly and you can do [self startSortingTheArray:myDataArray]
already.
On displaying the result in a tableview - consider turning your content into an array of dictionaries. You can easily get lost trying to do it with one flat array. Each dictionary can contain an array defining each row in the section as well as a title for the section. When it comes time to get counts of sections or rows, you just count the number of items in the top-level array (section count) or count the number of items in the array containing row information in the dictionary (row count).
int sectionCount = [sectionsArray count];
NSString *sectionTitle = [[sectionsArray objectAtIndex:sectionNumber] valueForKey:@"title"];
int rowCount = [[sectionsArray objectAtIndex:sectionNumber] valueForKey:@"rowContentArray"] count];
// rowObject might be a string, a NSValue, whatever contains the information you want in the table view cell
id rowObject = [[sectionsArray objectAtIndex:sectionNumber] valueForKey:@"rowContentArray"] objectAtIndex:rowNumber];
Another approach is to put all your received data in Core Data which you might have been planning on doing anyway if you require some kind of persistence. In that case it is really worth using a NSFetchedResultsController
.
精彩评论