2016-09-02 4 views
1

ここにはtextfieldの国の2つのボタンがあり、国のその他のボタンはtableViewです。 countrydropdownボタンをクリックすると、国リストが表示されます。statedropdownボタンをクリックすると、状態リストが表示されます。ボタンを押したときに1つのテーブルに2つの異なる配列のデータを表示する必要があります

スクリーンショット:

最初のスクリーンショットは、クリックする前の画面を表示します。

クリックした後の2番目の表示、データなしのドロップダウンリスト(tableView)が表示されます。

enter image description here
enter image description here

私が使用したコード。

@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); 

} 


} 
+0

tableviewセットアップコードを入力してください。 – Prathamesh

+0

sir参照用のスクリーンショットをアップロードしました。助けてください! –

+0

[_countryView reloadData]を削除します。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath tableviewdatasourceメソッド – Prathamesh

答えて

1

[_countryView reloadData]を削除する必要があります。 cellForRowAtIndexPath:メソッドからこのボタンを呼び出す方法ボタン操作方法から

+0

ありがとうそれは私のために働いた多くの:-) –

1

tableView'sを2つ使用し、以下のコードを参照してください。buttonクリックしてください。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // identify each tableView by its object 
    if(tableView == countryTabView){ 
      // don't return hard-coded value 
      return number of country array count; 
    } 
    if(tableView == stateTabView){ 
      // don't return hard-coded value 
      return number of state array count; 
    } 
} 

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

    // identify each tableView by its object 
    if(tableView == stateTabView){ 
     // code for stateTableView here 
    } 

    if(tableView == countryTabView){ 
     // code for countryTabView here 
    } 

    return cell; 
} 
関連する問題