在segmentedControl上更改UITableView的内容更改了索引

时间:2014-03-04 16:13:57

标签: ios uitableview

我已经尝试了一些我在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:

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

2 个答案:

答案 0 :(得分:0)

如果队列中没有可用的单元格,则应初始化单元格,然后更改单元格的值。类似的东西:

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

答案 1 :(得分:0)

不确定你的代码中是否遗漏了它,或者你只是忘了在你的问题上复制它,但是因为你是从代码中创建了segmentControl,所以你在某处添加了这个,对吗?

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