My code works fine, but only works for one cell. When I define 5 rows, it only works for the last cell. If I click on 1 cell, then the value displayimageon the last cell just does not show where I click, and which cell I click, how to process the button image with the mouse button for each cell, by clicking on the button the image of this code.
-(void)changeMapType:(id)sender
{
changeimagetype =!changeimagetype;
if(changeimagetype == YES)
{
onButtonView.image = [UIImage imageNamed:@"alarm_OF.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
}
else
{
onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
}
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (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;
onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
[mimageButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:@selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
}
Can you help me fix the code?
source
share