Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使uitableview的节标题中的按钮保持选中状态_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 如何使uitableview的节标题中的按钮保持选中状态

Ios 如何使uitableview的节标题中的按钮保持选中状态,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个按钮,我想改变背景图像,如果它被选中。我这里的代码可以工作,但如果我再次上下滚动,它将变为未选中 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width

我有一个按钮,我想改变背景图像,如果它被选中。我这里的代码可以工作,但如果我再次上下滚动,它将变为未选中

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

发生这种情况的原因是,当表格上下滚动时,它将再次创建单元格,所以您必须以某种方式管理它,选择或不选择哪个按钮,并再次将选定状态设置为以前在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];
}
//在头文件中定义
NSMUTABLEARRY*已选择MUTARRY;
//在viewdidload中初始化
mutArrSelected=[[NSMutableArray alloc]init];
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分
{
if(mutArrSelected.count<节)
{
[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=是;
}
button.tag=节;
[按钮setBackgroundImage:[UIImage ImageName:@“未选择的_图像”]用于状态:UIControlStateNormal];
[按钮setBackgroundImage:[UIImage ImageName:@“selected_image”]用于状态:UIControlStateSelected];
[按钮添加目标:自我操作:@选择器(按钮按下:)用于控制事件:UIControlEventTouchUpInside];
[节头添加子视图:按钮];
返回段标题;
}
-(无效)按钮按下:(id)发件人
{
UIButton*allButton=(UIButton*)发送方;
allButton.selected=!allButton.selected;
[mutArrSelected removeObjectAtIndex:allButton.tag];
[mutArrSelected insertObject:[NSNumber numberWithBool:allButton.selected]atIndex:allButton.tag];
}

这里发生的事情是,每次视图变为可见时,实际上都会创建一个全新的视图,并重置所有内容。不必这样做,您只需保存视图并每次重复使用相同的视图即可。改为在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中节的值为每个节返回正确的一个节标题视图数组。此数组跟踪照片的名称,无论是否单击

- (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) 您的
标题视图部分

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