ここにはtextfield
の国の2つのボタンがあり、国のその他のボタンはtableView
です。 countrydropdown
ボタンをクリックすると、国リストが表示されます。statedropdown
ボタンをクリックすると、状態リストが表示されます。ボタンを押したときに1つのテーブルに2つの異なる配列のデータを表示する必要があります
スクリーンショット:
最初のスクリーンショットは、クリックする前の画面を表示します。
クリックした後の2番目の表示、データなしのドロップダウンリスト(tableView
)が表示されます。
私が使用したコード。
@interface RegisterScreen(){
NSMutableArray *countryArray;
NSMutableArray *stateArray;
NSMutableArray *cityArray;
NSMutableArray *tempArray;
BOOL isCountry;
BOOL isState;
NSInteger count;
}
@end
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"touchesBegan:withEvent:");
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
- (void)viewDidLoad {
[super viewDidLoad];
[email protected]"Registration";
//setting up image in button
UIImage *buttonImage = [UIImage imageNamed:@"apple.png"];
[_selectimage setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
[self.registerDOB setInputView:datePicker];
countryArray = [[NSMutableArray alloc] initWithObjects:@"Afghanistan",@"Åland Islands",@"Albania",@"Algeria",@"India",@"American Samoa", nil];
stateArray = [[NSMutableArray alloc]initWithObjects:@"Gujarat",@"Maharashtra", @"Karnataka",nil];
}
-(void) dateTextField:(id)sender{
UIDatePicker *picker = (UIDatePicker*)self.registerDOB.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
self.registerDOB.text = [NSString stringWithFormat:@"%@",dateString];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)dropDownCountry:(id)sender {
isCountry =YES;
isState=NO;
count=countryArray.count;
NSLog(@"%ld",(long)count);
self.countryView.hidden = NO;
if (self.countryView.frame.size.height != 0) {
[UIView animateWithDuration:0.3 animations:^{
self.countryView.frame = CGRectMake(self.countryView.frame.origin.x, self.countryView.frame.origin.y, self.countryView.frame.size.width, 0);
} completion:^(BOOL finished) {
// self.dropDownImageView.highlighted = NO;
}];
}
else {
[UIView animateWithDuration:0.3 animations:^{
self.countryView.frame = CGRectMake(10, 404, 302, 100);
} completion:^(BOOL finished) {
// self.dropDownImageView.highlighted = YES;
}];
}
}
- (IBAction)dropDownState:(id)sender {
isState =YES;
isCountry=NO;
count=stateArray.count;
NSLog(@"%ld",(long)count);
self.countryView.hidden = NO;
if (self.countryView.frame.size.height != 0) {
[UIView animateWithDuration:0.3 animations:^{
self.countryView.frame = CGRectMake(self.countryView.frame.origin.x, self.countryView.frame.origin.y, self.countryView.frame.size.width, 0);
} completion:^(BOOL finished) {
// self.dropDownImageView.highlighted = NO;
}];
}
else {
[UIView animateWithDuration:0.3 animations:^{
self.countryView.frame = CGRectMake(10, 404, 302, 100);
} completion:^(BOOL finished) {
}];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 25;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if(isCountry){
// tempArray=countryArray;
static NSString *idenfier = @"countryList";
cell = [tableView dequeueReusableCellWithIdentifier:idenfier forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [countryArray objectAtIndex:indexPath.row];
[_countryView reloadData];
}else if (isState) {
static NSString *idenfier = @"countryList";
cell = [tableView dequeueReusableCellWithIdentifier:idenfier forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [stateArray objectAtIndex:indexPath.row];
[_countryView reloadData];
};
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
if(isCountry){
self.registerCountry.text = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
// self.countryView.hidden = YES;
self.countryView.frame = CGRectMake(self.countryView.frame.origin.x, self.countryView.frame.origin.y, self.countryView.frame.size.width, 0);
}else if (isState)
{
self.registerState.text = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];
// self.countryView.hidden = YES;
self.countryView.frame = CGRectMake(self.countryView.frame.origin.x, self.countryView.frame.origin.y, self.countryView.frame.size.width, 0);
}
}
tableviewセットアップコードを入力してください。 – Prathamesh
sir参照用のスクリーンショットをアップロードしました。助けてください! –
[_countryView reloadData]を削除します。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath tableviewdatasourceメソッド – Prathamesh