在tableview中点击部分时显示或隐藏行

时间:2012-03-13 05:36:33

标签: iphone ios uitableview

我使用- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section为tableview中的部分创建了一个自定义视图,但是在重新加载tableview的部分上点击了按钮。它为该部分重新加载表,但不显示剩余的部分。

我不知道该怎么做。 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section的代码是

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

if (!selected) {

    headerView = [[[HeaderView alloc] initWithFrame:HeaderFrame] autorelease];

    headerView.backgroundColor = [UIColor greenColor];

    headerView.label.text = @"title";

    headerView.button.tag = section;

    headerView.tag = section;

    [headerView.button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

     return headerView;
}
else if (selectedIndex == section && selected){

    [headerView.button setImage:[UIImage imageNamed:@"Croped-Rotated.png"] forState:UIControlStateNormal];

    return headerView;
}
else 
    return headerView;
}

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {



    headerView = [[[HeaderView alloc] initWithFrame:HeaderFrame] autorelease];

    headerView.backgroundColor = [UIColor greenColor];

    headerView.label.text = @"title";

    headerView.button.tag = section;

    headerView.tag = section;

    [headerView.button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

if (selectedIndex == section && selected){

    [headerView.button setImage:[UIImage imageNamed:@"Croped-Rotated.png"] forState:UIControlStateNormal];

    return headerView;
}
else 
    return headerView;
}

试试这个。它可能对你有帮助。

答案 1 :(得分:0)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

if (!selected) {

    headerView = [[[HeaderView alloc] initWithFrame:HeaderFrame] autorelease];

    headerView.backgroundColor = [UIColor greenColor];

    headerView.label.text = @"title";

    headerView.button.tag = section;

    headerView.tag = section;

    [headerView.button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

     return headerView;
}
else if (selectedIndex == section && selected){

    [headerView.button setImage:[UIImage imageNamed:@"Croped-Rotated.png"] forState:UIControlStateNormal];

    return headerView;
}
else 
    return headerView;
}

根据您的代码if (!selected),然后headerView获得其他明智的分配。 因此,在if条件之前执行分配工作正如@jyotishree所述。