简体   繁体   English

如何在UITableview中检查UIButtons的条件

[英]how to check the condition for UIButtons in UITableview

按钮1的状况按钮3 I am having three button image in table view, I want to check the conditions between them. 我在表格视图中有三个按钮图像,我想检查它们之间的条件。 when I click the button 1 means background image change to blue colour. 当我单击按钮1时,表示背景图像变为蓝色。 at the same time I click the button 1 it will move to normal state white colour. 同时单击按钮1,它将变为正常状态的白色。 Same for another two buttons. 其他两个按钮相同。

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

My condition is: when button 1 is clicked means button 3 should not change. 我的条件是:单击按钮1时,意味着按钮3不应更改。 when button 3 is clicked means button 1 should not change.button 2 can select in both the condition. 当单击按钮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];
}
}

can any one help me in coding. 谁能帮我编码。

First of all you can set different tag for each button's like 首先,您可以为每个按钮的样式设置不同的标签

 button.tag == 1,
 button.tag == 2,
 button.tag == 3

And then after you can write your radiobtn Action like this way.. 然后,您可以像这样编写radiobtn Action。

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

U can set tags to button 您可以将标签设置为按钮

button1.tag=1;
button2.tag=2;
button3.tag=3; //instead of tag 1 for all buttons

Take reference for buttons as button1,button2,button3 globally.. 全局将按钮的引用作为button1,button2,button3。

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

Hope it helps you...! 希望它可以帮助您...!

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

Can you have a try on this.. 你可以试一试吗..

Please check!! 请检查!! i have done same thing on my code. 我在代码上做了同样的事情。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM