Reputation: 189
I have made a drop down having a button and under it is a table view in which i show list in it. There are three drop down with this respective order, but1 with table1 , but2 with table2 and but3 with table3.when we click but1 it shows list of country in table1. when we click but2 it shows the cities name of the country we selected in but1 in table2 and when we click but3 it shows same country names in table3 and when i select country from but3 and table3 it shows country code in a textfield. The issue i'm facing is that , when i select country fro but3, but2 city name changes to country name that i have selected in but3. I'm finding this issue about more than 6 hours but i'm failed , kindly please help me where i'm doing mistake. my code is,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([ImageViewC isEqualToString:@"1"])
return [data count];
else if ([ImageViewC isEqualToString:@"2"]){
return [data1 count];
}else{
return [data3 count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([ImageViewC isEqualToString:@"1"]) {
cell.textLabel.text = [data objectAtIndex:indexPath.row];
}else if([ImageViewC isEqualToString:@"2"]){
cell.textLabel.text = [data1 objectAtIndex:indexPath.row];
} else if([ImageViewC isEqualToString:@"3"]){
cell.textLabel.text = [data3 objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",selectedCell.textLabel.text);
if ([ImageViewC isEqualToString:@"1"]) {
[self.but1 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
if ([selectedCell.textLabel.text isEqualToString:@"Pakistan"]) {
data1=[[NSArray alloc]initWithArray:Pak_city];
}else if ([selectedCell.textLabel.text isEqualToString:@"UAE"]){
data1=[[NSArray alloc]initWithArray:UAE_city];
}else{
data1=[[NSArray alloc]initWithArray:Oman_city];
}
}
else{
[self.but2 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}
if ([ImageViewC isEqualToString:@"3"])
{
[self.Country setTitle:[NSString
stringWithFormat:@"%@",selectedCell.textLabel.text]
forState:UIControlStateNormal];
if ([selectedCell.textLabel.text isEqualToString:@"PAKISTAN"]){
_code.text=pakCode;
}else if ([selectedCell.textLabel.text isEqualToString:@"UAE"]){
_code.text=uaeCode;
}else {
_code.text=omanCode;
}
}
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=YES;
}
Upvotes: 1
Views: 60
Reputation: 8322
Change your condition as below :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == table1)
return [data count];
else if (tableView == table2){
return [data1 count];
}else{
return [data3 count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == table1) {
cell.textLabel.text = [data objectAtIndex:indexPath.row];
}else if(tableView == table2){
cell.textLabel.text = [data1 objectAtIndex:indexPath.row];
} else if(tableView == table3){
cell.textLabel.text = [data3 objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",selectedCell.textLabel.text);
if (tableview == table1) {
[self.but1 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
if ([selectedCell.textLabel.text isEqualToString:@"Pakistan"]) {
data1=[[NSArray alloc]initWithArray:Pak_city];
}else if ([selectedCell.textLabel.text isEqualToString:@"UAE"]){
data1=[[NSArray alloc]initWithArray:UAE_city];
}else{
data1=[[NSArray alloc]initWithArray:Oman_city];
}
}
else if (tableview == table2){
[self.but2 setTitle:[NSString stringWithFormat:@"%@",selectedCell.textLabel.text] forState:UIControlStateNormal];
}
else if (tableview == table3){
{
[self.Country setTitle:[NSString
stringWithFormat:@"%@",selectedCell.textLabel.text]
forState:UIControlStateNormal];
if ([selectedCell.textLabel.text isEqualToString:@"PAKISTAN"]){
_code.text=pakCode;
}else if ([selectedCell.textLabel.text isEqualToString:@"UAE"]){
_code.text=uaeCode;
}else {
_code.text=omanCode;
}
}
table1.hidden=YES;
table2.hidden=YES;
table3.hidden=YES;
}
Upvotes: 1