IOS中的下拉列表问题

时间:2017-07-15 05:31:04

标签: ios objective-c uitableview

enter image description here

enter image description here

我已经下了一个按钮,下面是一个表格视图,其中我显示了列表。这个订单有三个下拉列表,但是1表示table1,but2表示table2,but3表3表。当我们点击but1时它会显示table1中的国家列表。当我们点击but2时,它显示我们在table2中选择的国家的城市名称,当我们点击but3时,它在表3中显示相同的国家名称,当我从but3和table3选择国家时,它在文本字段中显示国家代码。我面临的问题是,当我从but3选择国家,但是2城市名称更改为我在but3中选择的国家/地区名称。我发现这个问题超过6个小时但是我失败了,请帮助我,我在做错了。我的代码是,

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

}

1 个答案:

答案 0 :(得分:1)

更改您的条件如下:

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

}
相关问题