我在表视图中有三个按钮图像,我想检查它们之间的条件。当我点击按钮1表示背景图像变为蓝色。同时我点击按钮1它将移动到正常状态白色。另外两个按钮相同。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath![button3 condition][3]
{
static NSString *cellIdentifier = @"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UIButton *button1;
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
button1.tag = 1;
[button1 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button1 setSelected:true];
[cell.contentView addSubview:button1];
UIButton *button2;
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(160, 27, 36, 36);
[button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"e"ofType:@"png"]] forState:UIControlStateNormal];
button2.tag = 1;
[button2 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button2 setSelected:true];
[cell.contentView addSubview:button2];
UIButton *button3;
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
button3.tag = 1;
[button3 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button3 setSelected:true];
[cell.contentView addSubview:button3];
return cell;
}
我的条件是:当点击按钮1时,按钮3不应该改变。当点击按钮3时,按钮1不应该改变。按钮2可以同时选择条件。
- (void)radiobtn:(UIButton *)button
{
if(button.tag == 1)
{
[button setImage:[UIImage imageNamed:@"lblue.png"] forState:UIControlStateSelected];
}
if(button.tag == 2)
{
[button setImage:[UIImage imageNamed:@"eblue.png"] forState:UIControlStateSelected];
}
if(button.tag == 3)
{
[button setImage:[UIImage imageNamed:@"vblue.png"] forState:UIControlStateSelected];
}
}
任何人都可以帮我编码。
答案 0 :(得分:1)
首先,您可以为每个按钮设置不同的标签,例如
button.tag == 1,
button.tag == 2,
button.tag == 3
然后你就可以这样写radiobtn
动作了......
-(IBAction) radiobtn:(id)sender
{
UIButton *yourBtn = (UIButton *)[self.view viewWithTag:[sender tag]];
if(yourBtn.tag == 1) {
[yourBtn setImage:[UIImage imageNamed:@"lblue.png"] forState:UIControlStateSelected];
}
else if(yourBtn.tag == 2){
[yourBtn setImage:[UIImage imageNamed:@"eblue.png"] forState:UIControlStateSelected];
}
else{
[yourBtn setImage:[UIImage imageNamed:@"vblue.png"] forState:UIControlStateSelected];
}
}
答案 1 :(得分:0)
你可以将标签设置为按钮
button1.tag=1;
button2.tag=2;
button3.tag=3; //instead of tag 1 for all buttons
全局按钮1,按钮2,按钮3参考按钮。
NSLog(@"%@",[[sender superview] class]); //UITableViewCellContentView
NSLog(@"%@",[[[sender superview] superview] class]); //UITableViewCellScrollView
NSLog(@"%@",[[[[sender superview]superview]superview] class]); //UITableViewCell
- (void)radiobtn:(UIButton *)button
{
if(button.tag==1)
{
//handle
CustomizedCellView * cell = (CustomizedCellView*)[[[button superview]superview]superview];
if ([button.imageView.image isEqual:[UIImage imageNamed:@"lblue.png"]]) {
[button setImage:[UIImage imageNamed:@"lwhite.png"] forState:UIControlStateSelected];
}
for (UIButton *btn in cell.contentView.subviews) {
if (btn.tag==3) {
[btn setImage:[UIImage imageNamed:@"vwhite.png"] forState:UIControlStateSelected];
}
}
}
else if(button.tag==2)
{
//handle
if ([button.imageView.image isEqual:[UIImage imageNamed:@"eblue.png"]]) {
[button setImage:[UIImage imageNamed:@"ewhite.png"] forState:UIControlStateSelected];
}
}
else if(button.tag==3)
{
//handle
CustomizedCellView * cell = (CustomizedCellView*)[[[sender superview]superview]superview];
if ([button.imageView.image isEqual:[UIImage imageNamed:@"vblue.png"]]) {
[button setImage:[UIImage imageNamed:@"vwhite.png"] forState:UIControlStateSelected];
}
for (UIButton *btn in cell.contentView.subviews) {
if (btn.tag==2) {
[btn setImage:[UIImage imageNamed:@"lwhite.png"] forState:UIControlStateSelected];
}
}
}
}
希望它可以帮助你......!
答案 2 :(得分:0)
button1.tag=1; button2.tag=2; button3.tag=3;
-(void)radiobtn:(UIButton *)button
{
if(button.tag==1)
{
if(![button3 isSelected])
{
if([button1 isSelected])
button1.backgroundColor = [UIColor blueColor];
else
button1.backgroundColor = [UIColor whiteColor];
}
else
{
//No change
}
}
else if(button.tag==2)
{
if([button2 isSelected])
button2.backgroundColor = [UIColor blueColor];
else
button2.backgroundColor = [UIColor whiteColor];
}
else if(button.tag==3)
{
if(![button1 isSelected])
{
if([button3 isSelected])
button3.backgroundColor = [UIColor blueColor];
else
button3.backgroundColor = [UIColor whiteColor];
}
else
{
//No change
}
}
}
你可以尝试一下..
答案 3 :(得分:0)
请检查!!我在我的代码上做了同样的事情。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"UITableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here
[cell setSelectedBackgroundView:selectedBackgroundView];
self.tableview.separatorColor = [UIColor whiteColor];
if (_userResult.relationStatus == [arrAnswrs objectAtIndex:indexPath.row]){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}else if (_userResult.childrenStatus == [arrAnswrs objectAtIndex:indexPath.row]){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
[[UITableViewCell appearance] setTintColor:[UIColor redColor]];
cell.textLabel.text = [arrAnswrs objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Roboto" size:16];
cell.textLabel.textColor = UIColorFromRGB(0xe212121);
cell.backgroundColor = UIColorFromRGB(0xeaaea7);
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Select indexPath.section %d --- index %d", (int)indexPath.section, (int)indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *status = [arrAnswrs objectAtIndex:indexPath.row];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
int iSeletedButton = (int)selectButton.tag ;
NSLog(@"value %@ -- iSeletedButton %d", [arrAnswrs objectAtIndex:indexPath.row], iSeletedButton);
switch (iSeletedButton) {
case 1:
_userResult.relationStatus = status;
_labelFrstStatus.text = status;
_labelFrstStatus.textColor = [UIColor blackColor];
break;
case 2:
_userResult.childrenStatus = status;
_labelSecndStatus.text = status;
_labelSecndStatus.textColor = [UIColor blackColor];
break;
default:
break;
}
if (_userResult.relationStatus || _userResult.childrenStatus) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
else cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView reloadData];
}