DownPicker 选择时如何获取数组索引值
Posted
技术标签:
【中文标题】DownPicker 选择时如何获取数组索引值【英文标题】:DownPicker when selected how to get the Array Index Value 【发布时间】:2018-04-18 10:35:03 【问题描述】:我已经按照link 安装了 DownPicker。选择项目时,我需要获取 NSMutableArray 索引值。背后的原因是我有 2 个数组。一个用于国家/地区,一个用于代码。当用户选择国家时,我需要获取索引值来获取代码。任何帮助都将不胜感激。
- (void)viewDidLoad
[super viewDidLoad];
//=== Initiallize the Mutable Array
countryMArray = [[NSMutableArray alloc] init];
codeMArray = [[NSMutableArray alloc] init];
//=== Initialize the responseData Mutable Data
self.responseData = [NSMutableData data];
//=== Pass the string to web and get the return Country response.write
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:@"/apps/getcountry.asp?"];
NSURLRequest *requestCountry = [NSURLRequest requestWithURL:[NSURL URLWithString:sURL]];
(void) [[NSURLConnection alloc] initWithRequest:requestCountry delegate:self];
//=== Pass the string to web and get the return Code response.write
sURL = appDelegate.gURL;
sURL = [sURL stringByAppendingString:@"/apps/getctcode.asp?"];
NSURLRequest *requestCode = [NSURLRequest requestWithURL:[NSURL URLWithString:sURL]];
(void) [[NSURLConnection alloc] initWithRequest:requestCode delegate:self];
self.pickerCountry = [[DownPicker alloc] initWithTextField:self.txtCountry withData:countryMArray];
【问题讨论】:
【参考方案1】:在viewDidLoad
中添加一个目标到pickerCountry
[self.pickerCountry addTarget:self
action:@selector(pickerClicked:)
forControlEvents:UIControlEventValueChanged];
//
-(void)pickerClicked:(id)dp
NSString* selectedValue = [self.pickerCountry text];
for ( int i = 0 ; i < countryMArray.count; i++)
NSString*item = [countryMArray objectAtIndex:i];
if([item isEqualToString:selectedValue])
[self.pickerCode selectRow:i inComponent:0 animated:YES];
break;
【讨论】:
【参考方案2】:当用户滚动时,添加目标。您将通过所需的方法获得回调,如下所示:
[self.pickerCountry addTarget:self
action:@selector(dp_Selected:)
forControlEvents:UIControlEventValueChanged];
-(void)dp_Selected:(id)dp
NSString* selectedValue = [self.pickerCountry text];
// do what you want
//search the text to get index value
官方链接: https://github.com/Darkseal/DownPicker#status-change-event-handling
【讨论】:
以上是关于DownPicker 选择时如何获取数组索引值的主要内容,如果未能解决你的问题,请参考以下文章