【发布时间】:2020-03-22 14:42:36
【问题描述】:
我有两个部分的标题,每个标题都有 UIButton,当我点击标题中的 UIButtons 时,将打开一个弹出窗口。它有两个按钮是和否。当用户单击该弹出窗口中的“是”选项时,我想在“标题”按钮中显示“是”标题。 例如:如果用户点击第 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