如何选择uitableview的节标题中的按钮

时间:2014-08-26 09:13:18

标签: ios objective-c uitableview

我有一个按钮,我想要更改背景图像,如果它被选中。我在这里的代码有效但如果我再次向上和向下滚动它就会被取消选择。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
    UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    sectionHeaderLabel.text = [NSString stringWithFormat:@"Section %i",section+1];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
    [button setBackgroundImage:[UIImage imageNamed:@"unselected_image"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"selected_image"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [sectionHeader addSubview:button];
    return sectionHeader;
}

- (void)buttonPressed:(id)sender {
    UIButton *allButton = (UIButton*)sender;
    allButton.selected = !allButton.selected;
}

3 个答案:

答案 0 :(得分:4)

1)在ViewDidLoad中创建一个图像名称数组。此数组会跟踪您的照片名称,无论其是否已被点击。

- (void)viewDidLoad
{
    //this array is just an example, loop and add items to the array for the number of sections you want
    //in this example there are 5 sections with 5 images
    imageNameArray = [[NSMutableArray alloc] initWithObjects:@"unselected_image", @"unselected_image", @"unselected_image", @"unselected_image", @"unselected_image", nil];
}

2)你的viewForHeaderInSection

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
    UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    sectionHeaderLabel.text = [NSString stringWithFormat:@"Section %i",section+1];
    button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
    [button setBackgroundImage:[UIImage imageNamed:[imageNameArray objectAtIndex:section]] forState:UIControlStateNormal];
    [button setTag:section];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [sectionHeader addSubview:button];
    return sectionHeader;
}

3)如果按下按钮,请更改数组中图像的名称

- (void)buttonPressed:(id)sender
{
    UIButton *allButton = (UIButton*)sender;

    [imageNameArray insertObject:@"selected_image" atIndex:[allButton tag]];
    [allButton setBackgroundImage:[UIImage imageNamed:[imageNameArray objectAtIndex:[allButton tag]]] forState:UIControlStateNormal];
}

答案 1 :(得分:1)

发生这种情况是因为当表格向上和向下滚动时它会再次创建单元格,因此您必须以某种方式管理它,选择或不选择按钮,并再次将选定状态设置为先前在viewForHeaderInSection中选择的按钮,您可以这样做如吼叫

//Define in header file
NSMutableArray *mutArrSelected;

//Initlise in viewdidload
mutArrSelected = [[NSMutableArray alloc] init];


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

    if(mutArrSelected.count < section)
    {
        [mutArrSelected addObject:[NSNumber numberWithBool:NO]];
    }

    UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
    UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    sectionHeaderLabel.text = [NSString stringWithFormat:@"Section %i",section+1];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
    if([[mutArrSelected objectAtIndex:section] boolValue])
    {
        button.selected = YES;
    }
    button.tag = section;
    [button setBackgroundImage:[UIImage imageNamed:@"unselected_image"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"selected_image"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [sectionHeader addSubview:button];
    return sectionHeader;
}

- (void)buttonPressed:(id)sender
{
    UIButton *allButton = (UIButton*)sender;
    allButton.selected = !allButton.selected;

    [mutArrSelected removeObjectAtIndex:allButton.tag];
    [mutArrSelected insertObject:[NSNumber numberWithBool:allButton.selected] atIndex:allButton.tag];
}

答案 2 :(得分:1)

这里发生的是,每次视图变得可见时,您实际上都会创建一个全新的视图,并重置所有内容。而不是这样做,您可以简单地保存视图并每次重复使用相同的视图。改为在viewDidLoad中创建视图:

- (void)viewDidLoad
{
    sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 33)];
    UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    sectionHeaderLabel.text = [NSString stringWithFormat:@"Section %i",section+1];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 5, 42, 20)];
    [button setBackgroundImage:[UIImage imageNamed:@"unselected_image"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"selected_image"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [sectionHeader addSubview:button];
}

viewForHeaderInSection变为:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return sectionHeader;
}

请注意,如果您有多个部分,则需要创建一个部分标题视图数组,并根据viewForHeaderInSection中部分的值为每个部分返回正确的部分。 / p>