简体   繁体   English

如何在iPhone的单击操作中更改两个uibutton的背景颜色?

[英]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. 我是iPhone应用程序开发的新手。在我的应用程序中,我正在使用tableview。在tableview中,我使用了两个uibutton,一个为白色 ,另一个为grey 。如果单击灰色按钮,则应更改为白色,另一个按钮应同时更改为灰色。

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. 有什么办法可以改变uibutton的背景颜色。

The problem is that you're reloading the table immediately after the button is pressed by including: 问题在于,按下按钮后,您将立即通过以下方式重新加载表:

[profiletableview reloadData];

in your reloadAction: method. 在您的reloadAction:方法中。 By doing so, you're just resetting the colors to those initially defined in tableView:cellForRowAtIndexPath: . 这样,您就可以将颜色重置为最初在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). 并且,如果您需要能够重新加载表中的数据,则可能不应该在tableView:cellForRowAtIndexPath:方法中初始化UIButton(或者,至少在第一次加载之后,您不应重复初始化UIButton)。 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: . 您最好在viewDidLoad方法中初始化UIButton,以便在加载表之前对其进行初始化,然后,就像已经完成的那样,在tableView:cellForRowAtIndexPath:期间将UIButton添加到单元格,并在更改过程中更改按钮的颜色和值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; 我可能是错的,但是您可能仍然绝对不需要在reloadAction:期间重新加载表reloadAction:在这种情况下,因为您可以直接更改按钮,也可以使用相同的方法直接更改按钮标题; but, it's generally nice to have the option to reload your table without messing up your buttons. 但是,通常可以选择重新加载表格而不弄乱按钮,这是很好的选择。

Edit #2: 编辑#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. 另外,如果该行保持静态,则可以使用reloadRowsAtIndexPaths:withRowAnimation仅重新加载必要的行,或者,如KIDdAe所述,进行全表重新加载,但是如果cell == nil,则仅将按钮添加到该单元格。

First you will want to make a UIImage that is the color that you want the button to be. 首先,您将要制作一个UIImage,它是您希望按钮成为的颜色。 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. 运行后,您将拥有一个1x1像素的UIImage,在这种情况下为蓝色。 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. 然后,您可以轻松地将许多不同按钮的背景色设置为不同的颜色。

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

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