2012-08-27 1 views
0

외부 XML 파일에서 UIPickerView 데이터를 동적으로 채울 수 있습니다. 예를 들어 필자보기에서 TextA, TextB 및 TextC를 갖게되며 원격 서버의 XML 파일에 동일한 내용을 갖게됩니다. 필자는 성공적으로 원격 서버에서 XML 데이터를 읽고 텍스트 (제목과 URL)를 내 tableview에 표시했습니다. 하지만 지금 내 XML 파일에서 내 UIPickerView 데이터를 채우려고합니다. 이것의 주요 목적은 XML 파일에서 UIPickerView 데이터를 동적으로 제어하는 ​​것입니다. 어쨌든 이런 일이 발생 했습니까?UIPickerView는 외부 XML 파일에서 동적으로 데이터를 채 웁니다.

+0

는 XML이 필요하다? .plist 파일을 사용하는 것이 더 쉬울 것입니다. – DrummerB

+0

나중에 사용할 수 있습니다 .plist 있지만 내 외부 사이트에서 pickerview 내에서 데이터를 제어하려면 pickerview의 모든 데이터가 동적이며 미래에 변경됩니다. 최고의 디자인 접근 방식은 무엇입니까? – jeewan

답변

0

나는 제목을 표시하려면 다음을 수행 한 내 애플 리케이션 중 하나에서 URL을. 저도 같은 대신 테이블 셀의 선택기 데이터를 채우려 : 다음 코드의 구현을 보여줍니다 This is a link of one of my apps :

urlString = @"http://jeewanaryal.web44.net/iPhoneXML/nepaliMoviesNews.xml"; 
data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; 
dict = [XMLReader dictionaryForXMLData:data error:nil]; 

newsItems = [[NSMutableArray alloc] initWithArray:[[[dict objectForKey:@"list"] objectForKey:@"lists"] objectForKey:@"news"]]; 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

CustomCell *cell= [[CustomCell alloc] initWithFrame:CGRectMake(0, 0, 300, 60)]; 

// Default to no selected style and not selected 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 

// Set the image for the cell 
[cell setTheImage:[UIImage imageNamed:[NSString stringWithFormat:@"Arrow3.png"]]]; 

NSMutableArray *temp = [[NSMutableArray alloc] init]; 

for (NSDictionary *fruitDict in newsItems) { 


    //UILabel *songTitle = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 300, 40)]; 

    [temp insertObject:[NSString stringWithFormat:@"%@",[[fruitDict objectForKey:@"title"] objectForKey:@"text"]] 
       atIndex:0]; 
    //cell.textLabel.text = [NSString stringWithFormat:@"%@",[[fruitDict objectForKey:@"title"] objectForKey:@"text"]]; 


    //[self.view addSubview:songTitle]; 
} 

//NSLog(@"\n\n temp: \n %@",temp); 

cell.textLabel.text = [temp objectAtIndex:indexPath.row]; 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
cell.textLabel.font = [UIFont fontWithName:@"Gill Sans" size:20]; 
cell.textLabel.textColor = [UIColor colorWithRed:102.0/255 green:204.0/255 blue:170.0/255 alpha:1]; 
//cell.textLabel.textColor =[UIColor colorWithRed:(CGFloat)0.24 green:(CGFloat)0.7 blue:(CGFloat)0.45 alpha:(CGFloat)1]; 
cell.backgroundColor = [UIColor blackColor]; 
[[cell textLabel] setTextAlignment:UITextAlignmentLeft]; 
//cell.textLabel.adjustsFontSizeToFitWidth = YES; 
//cell.detailTextLabel.numberOfLines = 2; 

return cell; 
} 
1

NSXMLParser으로 XMl 파일을 구문 분석 할 수 있습니다. 이벤트 구동 파서이므로 모든 콜백을 처리하고 파싱 된 노드에서 배열과 사전을 만들 대리자를 설정해야합니다.

XMLReader과 같은 클래스를 사용할 수도 있습니다. 이를 통해 XML을 NSData 또는 NSString 객체로 전달하고 파싱 된 NSDictionary를 얻을 수 있습니다. 그런 다음이를 사용하여 UIPickerView를 채울 수 있습니다.

다소 불편합니다. 가능한 경우 .plist 개의 파일을 사용하는 것이 좋습니다. 물론 다른 플랫폼에서도 동일한 정보에 액세스하려는 경우 옵션이 아닙니다.

NSArray *onlineList = [plistAsString propertyList]; 

아니면 디스크에 파일을 다운로드하는 경우 :

그런 다음 파일을 다운로드 한 후해야 할 것입니다 모두는

NSArray *onlineList = [[NSArray alloc] initWithContentsOfFile:pathToPlistFile]; 
// release if not using ARC. 
+0

버려주세요. – jeewan

관련 문제