Change the background colour of UIbutton when selected in UITableView Xcode?

Change the background colour of UIbutton when selected in UITableView Xcode?

arizah

New Member
Thread Starter
Joined
Oct 12, 2012
Messages
6
Reaction score
0
I am displaying a NSMutableArray items in UITableViewCell acting like gridview.The thing is I am displaying text in UIButton by making it invisible.Now when a user click on button (its text) the colour of the button should change.This is to let user know that he has selected a particular button.Butwhen I am doing it what is happening is when I click on a button its colour changes to red .But when i click on the next button its colour is also changed to red but the prevoius buttons colour is also red.I want the prevoius button to come back to its normal state when another button is clicked .How can I do it? This is my code:
Code:
Code:
 - ([COLOR=#2B91AF]UITableViewCell[/COLOR] *)tableView:([COLOR=#2B91AF]UITableView[/COLOR] *)tableView cellForRowAtIndexPath:([COLOR=#2B91AF]NSIndexPath[/COLOR] *)indexPath {
 [COLOR=#2B91AF]CGRect[/COLOR] rect = [COLOR=#2B91AF]CGRectMake[/COLOR]([COLOR=#800000]18[/COLOR]+[COLOR=#800000]80[/COLOR]*j, yy,[COLOR=#800000]57[/COLOR], [COLOR=#800000]40[/COLOR]);
        [COLOR=#2B91AF]UIButton[/COLOR] *button=[[[COLOR=#2B91AF]UIButton[/COLOR] alloc] initWithFrame:rect];
        [button setFrame:rect];

        [button setContentMode:[COLOR=#2B91AF]UIViewContentModeCenter[/COLOR]];
        [COLOR=#2B91AF]NSString[/COLOR] *settitle=[[COLOR=#2B91AF]NSString[/COLOR] stringWithFormat:@[COLOR=#800000]"%@"[/COLOR],item.title];
        [button setTitle:settitle forState:[COLOR=#2B91AF]UIControlStateNormal[/COLOR]];
        [COLOR=#2B91AF]NSString[/COLOR] *tagValue = [[COLOR=#2B91AF]NSString[/COLOR] stringWithFormat:@[COLOR=#800000]"%d%d"[/COLOR], indexPath.section+[COLOR=#800000]1[/COLOR], i];
        button.tag = [tagValue intValue];
        [button setTitleColor:[[COLOR=#2B91AF]UIColor[/COLOR] blackColor] forState:[COLOR=#2B91AF]UIControlStateNormal[/COLOR]];              
        [button addTarget:[COLOR=#00008B]self[/COLOR] action:[COLOR=#800000]@selector[/COLOR](buttonPressed:) forControlEvents:[COLOR=#2B91AF]UIControlEventTouchUpInside[/COLOR]];
        [hlcell.contentView addSubview:button];
        [button release];
}

-([COLOR=#2B91AF]IBAction[/COLOR])buttonPressed:(id)sender {
[COLOR=#00008B]int[/COLOR] tagId = [sender tag];
[COLOR=#00008B]int[/COLOR] divNum = [COLOR=#800000]0[/COLOR];
[COLOR=#00008B]if[/COLOR](tagId<[COLOR=#800000]100[/COLOR])
    divNum=[COLOR=#800000]10[/COLOR];
[COLOR=#00008B]else[/COLOR] 
    divNum=[COLOR=#800000]100[/COLOR];
[COLOR=#00008B]int[/COLOR] section = [sender tag]/divNum;
section -=[COLOR=#800000]1[/COLOR]; 
[COLOR=#00008B]int[/COLOR] itemId = [sender tag]%divNum;
[COLOR=#2B91AF]UIButton[/COLOR] *button = ([COLOR=#2B91AF]UIButton[/COLOR] *)sender;
[COLOR=#00008B]if[/COLOR](button.enabled==[COLOR=#00008B]true[/COLOR])

{
   button.backgroundColor=[[COLOR=#2B91AF]UIColor[/COLOR] redColor];
}

[COLOR=#2B91AF]NSLog[/COLOR](@[COLOR=#800000]"…section = %d, item = %d"[/COLOR], section, itemId);
[COLOR=#2B91AF]NSMutableArray[/COLOR] *sectionItems = [sections objectAtIndex:section];
[COLOR=#2B91AF]Item[/COLOR] *item = [sectionItems objectAtIndex:itemId];
[COLOR=#2B91AF]NSLog[/COLOR](@[COLOR=#800000]"..item pressed…..%@, %@"[/COLOR], item.title, item.link);

}


How can I do it?
 

Latest posts

Back
Top