外部 XML ファイルから UIPickerView データを動的に設定することは可能ですか。たとえば、ピッカー ビューに TextA、TextB、および TextC があり、リモート サーバーの XML ファイルにも同じものがあります。リモートサーバーからXMLデータを読み取り、テーブルビューにテキスト(タイトルとURL)を表示することに成功しました。しかし今、XML ファイルから UIPickerView データを入力しようとしています。これの主な目的は、XML ファイルから UIPickerView データを動的に制御することです。とにかくこれを実現する方法はありますか?
2 に答える
1
を使用して XML ファイルを解析できますNSXMLParser
。これはイベント ドリブン パーサーであるため、デリゲートを設定してすべてのコールバックを処理し、解析されたノードから配列と辞書を作成する必要があります。
XMLReaderのようなクラスを使用することもできます。これにより、xml を NSData または NSString オブジェクトとして渡すだけで、解析済みの NSDictionary を取得できます。その後、それを使用して UIPickerView を設定できます。
.plist
少し不便ですが、可能であればファイルを使用する方が良いでしょう。もちろん、他のプラットフォームからも同じ情報にアクセスしたい場合、それはオプションではありません。
次に、ファイルをダウンロードした後に行う必要があるのは、次のとおりです。
NSArray *onlineList = [plistAsString propertyList];
または、ファイルをディスクにダウンロードする場合:
NSArray *onlineList = [[NSArray alloc] initWithContentsOfFile:pathToPlistFile];
// release if not using ARC.
于 2012-08-27T19:07:58.220 に答える
0
アプリの1つにタイトルとURLを表示するために、次のことを行いました。テーブルセルの代わりにピッカーデータを入力するには、同じものが必要です: これは、次のコードの実装を示す私のアプリの 1 つのリンクです:
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;
}
于 2012-08-27T23:28:55.167 に答える