Possible duplicate:
how to handle the Google button on each and every one in tableviewcell on iPhone
Hi friend, I try this code, but my tableviewcell is reused when I scroll, and my image, which is turned on when clicked, disappears. I create a toggle button to call up an image in a cell
I want to show my image on the button that is on the cell, when I scroll the cell, then my image should remain in that cell of the button that the cell was clicked on.
-(void)changeMapType:(UIButton*)sender
{
changeimagetype =!changeimagetype;
sender.selected = changeimagetype;
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 10, 20, 20);
mimageButton.tag = 1;
[mimageButton setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];
[mimageButton setImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateSelected];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:@selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
[onButtonView release];
}
Rocky source
share