Harshit Goel
Harshit Goel

Reputation: 679

Need to show data of two different array in a single tableView when the buttons are pressed

here are two buttons next to textfield one for country and other for state i have used single tableView. when i click on countrydropdown button the country list should get displayed.and if i click statedropdown button the state list should get displayed.

Screens shots:

First screenshot displays the screen before the click.

Second displays after the click, dropdown(tableView) list occurs without any data.

enter image description here
enter image description here

Code I used.

@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];
self.navigationItem.title=@"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);

}


}

Upvotes: 1

Views: 105

Answers (2)

vaibhav
vaibhav

Reputation: 4096

Use two different tableView's and load data accordingly see below code then show them according to button click.

- (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;
}

Upvotes: 1

Jeyamahesan
Jeyamahesan

Reputation: 1111

you have to remove [_countryView reloadData]; from cellForRowAtIndexPath: method and call this reloadData from button action method

Upvotes: 1

Related Questions