Display dynamic data in UIPickerView in iPhone
I am using NSXMLParsing and UIPickerView in my application but i am not able to display data in uipicker view after parsing. can anyone help me out about this problem. I just want to know that how to store data in array and display it in uipickerview.
thnx frnds Thanks mahboudz... I have done that what u said but its not working.. actually i have parsed some data and stored it in nsmutablearray using dictionary... and want to display it in uipicker view but its not working.. my code is:-
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ( [elementName isEqualToString:@"news"] ) {
//msgAdded = [[attributeDict objectForKey:@"added"] retain];//fetch session from message tag.
//msgId = [[attributeDict objectForKey:@"id"] intValue];//fetch id from message tag.
newId = [[NSMutableString alloc] init];
newsTitle =[[NSMutableString alloc]init];
newsOverview = [[NSMutableString alloc]init];
newsOfDay = [[NSMutableString alloc] init];
createdOn = [[NSMutableString alloc]init];
newsDescription = [[NSMutableString alloc]init];
idNews = NO;
ttlNews = NO;
overvwNews = NO;
dyNews = NO;
onCreated = NO;
desNews = NO;
//inFromUserName = NO;
}
if ( [elementName isEqualToString:@"news_id"] ) {
idNews = YES;
}
if ( [elementName isEqualToString:@"news_title"] ) {
ttlNews = YES;
}
if ( [elementName isEqualToString:@"news_overview"] ) {
overvwNews = YES;
}
if ( [elementName isEqualToString:@"is_news_of_the_day"] ) {
dyNews = YES;
}
if ( [elementName isEqualToString:@"created_on"] ) {
onCreated = YES;
}
if ( [elementName isEqualToString:@"news_description"] ) {
desNews = YES;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ( idNews ) {
[newId appendString:string];
}
if (ttlNews) {
[newsTitle appendString:string];
}
if ( overvwNews ) {
[newsOverview appendString:string];
//NSLog(@"USER_NAME ===== %d",fromUserName);
}
if (dyNews) {
[newsOfDay appendString:string];
}
if (onCreated) {
[createdOn appendString:string];
}
if (desNews) {
[newsDescription appendString:string];
// NSLog(@"MESSAGE ==== %@",chatComment);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ( [elementName isEqualToString:@"news"] ) {
//[news addObject:[NSDictionary dictionaryWithObjectsAndKeys:chatSession,@"chatSession",fromUserName,@"fromUserName",chatComment,@"chatComment",nil]];
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:newsTitle,@"title",newId,@"id",nil]];
}
if ( [elementName isEqualToString:@"news_id"] ) {
idNews = NO;
}
if ( [elementName isEqualToString:@"news_title"] ) {
ttlNews = NO;
}
if ( [elementName isEqualToString:@"news_overview"] ) {
overvwNews = NO;
}
if ( [elementName isEqualToString:@"is_news_of_the_day"] ) {
dyNews = NO;
}
if ( [elementName isEqualToString:@"created_on"开发者_JAVA技巧] ) {
onCreated = NO;
}
if ( [elementName isEqualToString:@"news_description"] ) {
desNews = NO;
}
}
and now i want to display the contents of array in uipicker view. that is objectForKey:@"newstitle"
i have tried it display through following code:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *partyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
partyLabel.text= [[messages objectAtIndex:row]objectForKey:@"title"];
partyLabel.font = [UIFont boldSystemFontOfSize:13];
partyLabel.textAlignment = UITextAlignmentCenter;
partyLabel.backgroundColor = [UIColor clearColor];
return partyLabel;
}
but its not working ...please can any one help me out..
Thnx Kunal
You are free to store the parsed data in whatever format you wish, such as an NSArray or a C array. When the UIPicker needs to display each row, it will call one of the following, depending on how you are managing the Picker:
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
You need to implement only one of the two. The first one is easiest. When you get called for each row, then just return the NSString that you would like to have displayed in that row. The second one is more complex, but allows you to have more flexibility such as formatting the row or adding images.
If you are changing the display data after the UIPicker is on screen, you will need to call reloadComponent: or reloadAllComponents to update the display.
I think that at first you have to parse the xml and store data into an nsmutable array, then you have to display data in picker view from your array
精彩评论