在Tableview标题中查找UIButton并更改按钮标题

问题描述 投票:0回答:1

我有两部分的标题,每个标题都有UIButton,当我点击标题中的UIButton时,将打开一个弹出窗口。它有两个按钮“是”和“否”。当用户单击该弹出窗口中的“是”选项时,我想在“标题”按钮中显示“是”标题。例如:如果用户点击第1部分的标题按钮,将打开弹出窗口,并选择“是”。我想在我的“第1部分”标题按钮磁贴中显示Y $ S文本。

我的代码:

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

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
         view.backgroundColor = [UIColor whiteColor];

         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, tableView.frame.size.width-40, 21)];
         label.textAlignment = NSTextAlignmentLeft;

         label.font = [UIFont fontWithName:kAppFontLight size:12];
         label.textColor = [UIColor LSBlackColor];
         NSString *string = NSLocalizedString(@"Button1", nil);
         label.text = string;
         UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 48, tableView.frame.size.width, 1)];
         line.backgroundColor = [UIColor groupTableViewBackgroundColor];
 operate = [[UIButton alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 100, 6, 100, 30)];
 [operate setTitle:@"Button1" forState:UIControlStateNormal];
 [operate setTitleColor:[UIColor colorWithRed: 65/255.0 green: 190/255.0 blue: 236/255.0 alpha:1] forState:UIControlStateNormal];
 operate.titleLabel.font = [UIFont fontWithName:kAppFontRegular size:14];
 operate.tag = 10;
 [operate addTarget:self action:@selector(operateAction:) forControlEvents:UIControlEventTouchUpInside];

         [view addSubview:label];
         [view addSubview:line];
         [view addSubview:operate];
         return view;
}
else if (section == 1) {
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
         view.backgroundColor = [UIColor whiteColor];

         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, tableView.frame.size.width-40, 21)];
         label.textAlignment = NSTextAlignmentLeft;

         label.font = [UIFont fontWithName:kAppFontLight size:12];
         label.textColor = [UIColor LSBlackColor];
         NSString *string = NSLocalizedString(@"Button1", nil);
         label.text = string;
 UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 48, tableView.frame.size.width, 1)];
    line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    operateandEdit = [[UIButton alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 120, 6, 100, 30)];
    [operateandEdit setTitle:@"Button2" forState:UIControlStateNormal];
    [operateandEdit setTitleColor:[UIColor colorWithRed: 65/255.0 green: 190/255.0 blue: 236/255.0 alpha:1] forState:UIControlStateNormal];
    operateandEdit.titleLabel.font = [UIFont fontWithName:kAppFontRegular size:14];
    operateandEdit.tag = 11;
    [operateandEdit addTarget:self action:@selector(operateAction:) forControlEvents:UIControlEventTouchUpInside];

         [view addSubview:label];
         [view addSubview:line];
         [view addSubview:operateandEdit];
         return view;
}
return nil;
}

正在操作中:

-(void) operateAction:(id) sender {
if (operate.tag == 10) {
    if (sender.tag == 5) {
        [operate setTitle:@"YES" forState:UIControlStateNormal];
    } else {
        [operate setTitle:@"NO" forState:UIControlStateNormal];
    }
} else {
    if (sender.tag == 5) {
        [operateandEdit setTitle:@"YES" forState:UIControlStateNormal];
    } else {
        [operateandEdit setTitle:@"NO" forState:UIControlStateNormal];
    }
}
[self cancelButtonClicked:@""];
NSLog(@"%@", sender.currentTitle);
}
ios objective-c uitableview cocoa-touch
1个回答
0
投票
因此,按照我的逻辑,您必须对所有四个按钮使用不同的标签,然后使用它。

像下面一样,

if (sender.tag == 5) { [operate setTitle:@"YES" forState:UIControlStateNormal]; } else if (sender.tag == 6) { [operate setTitle:@"NO" forState:UIControlStateNormal]; } else if (sender.tag == 7) { [operateandEdit setTitle:@"YES" forState:UIControlStateNormal]; } else { [operateandEdit setTitle:@"NO" forState:UIControlStateNormal]; }

© www.soinside.com 2019 - 2024. All rights reserved.