简体   繁体   中英

How to change the background color of two uibutton on single click action in iPhone?

I'm new to iPhone app developing.In my app I'm using tableview.In tableview I'm using two uibuttons one in white color and another one in graycolor .If I click gray color button it should changed to white color and another button should changed to gray color on same time.

This is my code:

       -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    ExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell=nil;
    if(cell == nil)
     {
         cell = [[ExampleCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier];
     }

    if(indexPath.row == 0)
    {
        postbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [postbtn setFrame:CGRectMake(0, 205, 160, 34)];
        [postbtn setBackgroundColor:[UIColor whiteColor]];
        [postbtn setTitle:[NSString stringWithFormat:@"%@ POST",[AppDelegate.profileDic objectForKey:@"posts"]] forState:UIControlStateNormal];
        [postbtn.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:10]];
        [postbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [postbtn setTag:1001];
        [postbtn addTarget:self action:@selector(reloadAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:postbtn];

        likebtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [likebtn setFrame:CGRectMake(160, 205, 160, 34)];
        [likebtn setBackgroundColor:[UIColor lightGrayColor]];
        [likebtn setTitle:[NSString stringWithFormat:@"%@ LIKE",[AppDelegate.profileDic objectForKey:@"likes"]] forState:UIControlStateNormal];
        [likebtn.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:10]];
        [likebtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [likebtn setTag:1002];
        [likebtn addTarget:self action:@selector(reloadAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:likebtn];
        }
)};

    -(void)reloadAction:(id)sender
    {


        if ([sender tag] == 1001)
        {
           [postbtn setBackgroundColor:[UIColor whiteColor]];
           [likebtn setBackgroundColor:[UIColor lightGrayColor]];
            AppDelegate.selectedTab=1;
            [profiletableview reloadData];

        }
        if ([sender tag] == 1002)
        {
            [postbtn setBackgroundColor:[UIColor lightGrayColor]];
            [likebtn setBackgroundColor:[UIColor whiteColor]];
            AppDelegate.selectedTab=2;
            [self likewebservice];
            [profiletableview reloadData];
        }
    }

Is there any way to change the background color of uibutton.

The problem is that you're reloading the table immediately after the button is pressed by including:

[profiletableview reloadData];

in your reloadAction: method. By doing so, you're just resetting the colors to those initially defined in tableView:cellForRowAtIndexPath: . Take out that line and it should work.


Edit:

And if you need to be able to reload the data in the table, you probably shouldn't initialize the UIButtons in your tableView:cellForRowAtIndexPath: method (or at least, you shouldn't initialize the UIButtons repeatedly after the first load). You may be best off initializing your UIButtons in the viewDidLoad method so they're initialized before the table loads, then, as you've already done, adding the UIButtons to the cell during tableView:cellForRowAtIndexPath: and changing the button colors and values during reloadAction: . I could be wrong, but you probably still won't absolutely need to reload the table during reloadAction: in this case, since you're directly changing the buttons and can also directly change the button titles in this same method; but, it's generally nice to have the option to reload your table without messing up your buttons.

Edit #2:

Additionally if that row remains static, you can reload only the necessary rows using reloadRowsAtIndexPaths:withRowAnimation or, as KIDdAe said, do a full table reload, but only add the buttons to the cell if the cell==nil.

First you will want to make a UIImage that is the color that you want the button to be. You can do that with the following code:

CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

After you run that you'll have a 1x1 pixel UIImage that is, in this case, blue. Then you will set that image as the background image for the button state that you're setting. The following will set the image for the normal (unchecked) state of the button:

[yourbutton setBackgroundImage:image forState:UIControlStateNormal];

You can set a custom color for the clicked state as well, using a different color above and then doing the following:

[yourbutton setBackgroundImage:image forState:UIControlStateHighlighted];

You could put the code for creating the image in a separate method so that you just pass it the color that you want it to make the image. Then you can set the background colors of many different buttons to different colors easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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