views:

1367

answers:

3

I have a simple problem ... but its turning into a very difficult problem .. i have a button inside uitableviewcell and want to change its color through code .... i could aceive it by changing its background image. But its kinna hazy ... its not the solid color ... and when i use [cell.button setBackgroundColor:[UIcolor redColor]] ... the button color does not change ...

any help appreciated

thanks

A: 

I am not sure about how are you doing this, but if it is possible, why don't you keep reference to this button.

You can initialize it, insert it on your table and all the time that you need to change something, you use the variable that you have initialized.


// On your .h
UIButton *myButton;

// On your .m
// Initialize it.


// when displaying cells
cell.button = myButton;


// when you need to change the color use your myButton reference
myButton.backgroundColor = ...... 

Cheers,
VFN

vfn
hey thanks ... somebody pointed out that .. UIButton is inherited from UIView ... so the background property belogs to UIView and will not change the color ...when i am doing cell.button .. then button is UIButton IBOutlet to the real button on the xib Any other ideas?
KK
Hi. So, the button will inherit the UIView BG because you are clearing the background color. If you want to change the color for something different from the UIView BG, you will need to set some color.
vfn
A: 

If you have a background image (correct me if I got it wrong) then there is no chance to change the background color (or, at least, see this change) without changing the background image...

If you don't have a BG image then it should work...

Where do you change the color?
Inside the tableView:cellForRowAtIndexPath: or do you have a reference to the button?

UPDATE:

Haven't noticed the [cell.button.
What is cell?

Michael Kessler
hey thanks ... cell's the UITableViewCell instance that i create and return in cellForRowAtIndexPath method and button is IBOutlet for the roundbutton on the xibIf i use the image .. i do get green and red button ... but its not in its true color ... its very very hazy .... and somebody pointed out that .. UIButton is inherited from UIView ... so the background property belogs to UIView and will not change the color ... Any other ideas?
KK
Now I think that I understand your problem. Correct me if I'm wrong. You have a round-rect button and you want to change its background color so the button will still have transparent rounded corners. If this is the case then I would advise you to switch images. I don't think that there is a real app in the air that uses round-rect button and not a custom one with image...
Michael Kessler
A: 

Hi there!

I have an issue with the background image of button which is dependent of button state. I have a button inside customized table view cell and i have set different images for button's different states. Please look into the code below.

[btnNow setImage:[UIImage imageNamed:@"now_norm.png"] forState:UIControlStateNormal]; [btnNow setImage:[UIImage imageNamed:@"now_focus.png"] forState:UIControlStateHighlighted];

Whenever i tap on the actual button this works great but if i tap on area outside of button but which is still inside that same cell then this button changes its background image from UIControlStateNormal to UIControlStateHighlighted.

If i remove background image from UIControlStateHighlighted state then this issue doesn't exists but i need pressed state of the button.

Please help me out.

Thanks in advance!

Vivek Dandage