0
votes

I am displaying a NSMutableArray items in UITableViewCell acting like gridview.There are custom UIbuttons in a cell.Now when a user click on one button I want it to get highlighted.But when 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 remain unhighlighted and the present button to be highlighted .How can I do it? This is my code:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 CGRect rect = CGRectMake(18+80*j, yy,57, 40);
        UIButton *button=[[UIButton alloc] initWithFrame:rect];
        [button setFrame:rect];

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

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

{
   button.backgroundColor=[UIColor redColor];
}

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

}

How can I do it?

1
y cant change button when you click on it in ibaction method - ganesh manoj
Sorry I didn't get u .Could u please explain me briefly - Honey
you have given action to action there we are changing background color instead that change the button at that particular cell - ganesh manoj
Im pretty much new to objective-C.Can u please elaborate and tell or give me an idea of code - Honey

1 Answers

0
votes

one way to do it, is to keep an IBCollection reference for all your buttons in your header file (hook up all your buttons in Interface builder to this IBOutletCollection):

@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *allOfMyButtons;

and in your buttonPressed method make the background color red only if its the currently pressed button AND that button is enabled (all others turn back to black or whatever the initial color was):

-(IBAction)buttonPressed:(id)sender {
    [self.allOfMyButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIButton *button = (UIButton *)obj;

        if (button != sender && button.enabled) {
            [button setBackgroundColor:[UIColor redColor]];
        } else {
            [button setBackgroundColor:[UIColor blackColor]];
        }
    }];
}