I have an iPhone app problem that’s been bugging me for a few days and it really doesn’t seem like it should be this difficult so I’m sure I’m missing something obvious. I have researched plenty of forum discussions on “similar” topics but nothing that actually addresses this issue, specifically.
To be clear, if there is some piece of documentation or some other source that I should research, please point me in the right direction.
Here goes…
I have a list of items that I display to the user within a table (uitableview). The cell (uitableviewcell) for each item is custom and contains two image buttons(uibuttons: green and red). As expected, for each item in the the table, the user can click any of the buttons. Based on a parameter called monitoringRequestType for a button, the button calls a separate process to update the server. If the state is “Approved”,the image changes to ‘alreadyapproved’ and ‘reject’ respectively. So when I click on the red button, server updates the state to “Rejected” and image then changes to ‘approve’ and ‘alreadyrejected’.
Simple, right?
So, here is the issue:
On clicking the reject button, the ‘approve’ image comes on top of the ‘alreadyapproved’ image (so both images can be seen) while ‘alreadyrejected’ image works fine.For brevity, I am only including the relevant code here (hopefully formatted properly):
CellForRow:
if(indexPath.section==0){
NSDictionary *dict=[saveJson objectAtIndex:indexPath.row];
NSString* sMonitoringType = [dict valueForKey:@"monitoringType"];
UIButton *button1= [[UIButton alloc] initWithFrame:CGRectMake(230,10,40,40)];
UIButton *button2= [[UIButton alloc] initWithFrame:CGRectMake(280,10,40,40)];
if([sMonitoringType compare:@"Pending"] == NSOrderedSame){
[button1 setImage:[UIImage imageNamed:@"approve"]
forState:UIControlStateNormal];
[button1 addTarget:self
action:@selector(greenButtonPressed:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
button1.tag= indexPath.row;
[button2 setImage:[UIImage imageNamed:@"reject"]
forState:UIControlStateNormal];
[button2 addTarget:self
action:@selector(redButtonPressed:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
button2.tag= indexPath.row;
} else if([sMonitoringType compare:@"Approved"] == NSOrderedSame){
[button1 setImage:[UIImage imageNamed:@"alreadyapproved"]
forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:@"reject"]
forState:UIControlStateNormal];
[button2 addTarget:self
action:@selector(redButtonPressed:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
button2.tag= indexPath.row;
} else if([sMonitoringType compare:@"Rejected"] == NSOrderedSame){
[button1 setImage:[UIImage imageNamed:@"approve"]
forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:@"alreadyrejected"]
forState:UIControlStateNormal];
[button1 addTarget:self
action:@selector(greenButtonPressed:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
button1.tag= indexPath.row;
}
[cell addSubview:button1];
[cell addSubview:button2];
[button1 release];
[button2 release];
}
return cell;
}
The
UIButtons are being added to the cell’s subview whenever the cell reloads (you’re reusing cells, right?). You should reuse the older buttons already added to the cell’s subview instead of adding new ones and just to verify the issue, stop reusing the cells (memory inefficient, should not be done in production code!). To reuse the older buttons, set a unique tag for each button and get them back as[cell viewWithTag:uniqueTag]