Ios 更改分段控件更改索引上UITableView的内容

Ios 更改分段控件更改索引上UITableView的内容,ios,uitableview,Ios,Uitableview,我尝试了在stackoverflow上发现的几种方法,但在segmentedIndex更改时不会更改tableView的内容。我做错了什么 - (IBAction)segmentControlValueChanged:(UISegmentedControl *)sender { //Do something [self.theTableView reloadData]; } - (UITableViewCell *)tableView:(UITableView *)table

我尝试了在stackoverflow上发现的几种方法,但在segmentedIndex更改时不会更改tableView的内容。我做错了什么

- (IBAction)segmentControlValueChanged:(UISegmentedControl *)sender
{
    //Do something
    [self.theTableView reloadData];
}


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

        cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];
        if (cell == nil) {
            cell = [[GroupCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: @"cell"];
    if (segmentedControl.selectedSegmentIndex == 0) {




            cell.team = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 50, 50)];

            cell.team.text = @"team1";
            cell.team.font = [UIFont systemFontOfSize:14.0];
            cell.team.textColor = [UIColor blackColor];
            [cell.contentView addSubview:cell.team];



            cell.team = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 50, 50)];

            cell.position.text = @"1";
            cell.position.font = [UIFont systemFontOfSize:14.0];
            cell.position.textColor = [UIColor blackColor];
            [cell.contentView addSubview:cell.position];


    } else {

        cell.team.text = @"team2";
        cell.team.font = [UIFont systemFontOfSize:14.0];
        cell.team.textColor = [UIColor blackColor];
        [cell.contentView addSubview:cell.team];
    }



    }
    return cell;

}
ViewDidLoad分段控件:

segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Groups", @"Fixtures", nil]];



segmentedControl.frame = CGRectMake(-3, 0, self.view.frame.size.width+6, 40);
segmentedControl.selectedSegmentIndex = 0;

segmentedControl.tintColor =
 [UIColor colorWithRed: 13 / 255.0f
                 green: 78 / 255.0f
                  blue: 102 / 255.0f
                 alpha: 1.0f];



[self.view addSubview:segmentedControl];

当队列中没有可用单元格时,应初始化单元格,然后更改单元格的值。比如:

- (IBAction)segmentControlValueChanged:(UISegmentedControl *)sender
{
    //Do something
    [self.theTableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Try to get an already initialized cell from the queue
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) { //No Cell found, create one
        cell = [[GroupCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.team = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 50, 50)];
        cell.position = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 50, 50)];
        [cell.contentView addSubview:cell.team];
        [cell.contentView addSubview:cell.position];
    }

    //Now let's fill the values in our (initialized) cell
    if (segmentedControl.selectedSegmentIndex == 0) {
        cell.team.text = @"team1";
        cell.team.font = [UIFont systemFontOfSize:14.0];
        cell.team.textColor = [UIColor blackColor];
        cell.position.text = @"1";
        cell.position.font = [UIFont systemFontOfSize:14.0];
        cell.position.textColor = [UIColor blackColor];
    } else {
        cell.team.text = @"team2";
        cell.team.font = [UIFont systemFontOfSize:14.0];
        cell.team.textColor = [UIColor blackColor];
    }
    return cell;
}

不确定您的代码中是否缺少它,或者您只是忘记在问题中复制它,但是由于您是从代码创建segmentControl,所以您在某个地方添加了它,对吗

[segmentedControl addTarget:self
                     action:@selector(segmentControlValueChanged:)
           forControlEvents:UIControlEventValueChanged];

您的代码仅在cell==nil时运行。因此,更改
UISegmentedControl
不会更改已加载(出列)单元格的内容。如果您有自定义单元格类,为什么它不负责设置自己的子视图?视图控制器不应该这样做。当我按下“更改分段索引”按钮时,不会发生任何事情,因为它添加了更多相关信息