How to handle the Google button on each and every one on tableviewcell in iPhone

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;
//    sender.selected = changeimagetype;

    //sender.selected
    changeimagetype =!changeimagetype;
    if(changeimagetype == YES)
    {
        //flagButton=1;
        //[check setImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateNormal];
    onButtonView.image = [UIImage imageNamed:@"alarm_OF.png"];
    [mimageButton setImage:onButtonView.image forState:UIControlStateNormal];

    }
    else
    {
        //flagButton=2;

    onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
    [mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
    }
    [self.tableView reloadData];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


// Customize the appearance of table view cells.
- (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 = [UIButton buttonWithType:UIButtonTypeCustom];
        //[mimageButton setImage:[UIImage imageNamed:@"alarm_ON.png"] //forState:UIControlStateNormal];
        //[mimageButton setImage:[UIImage imageNamed:@"alarm_OF.png"] //forState:UIControlStateSelected];

        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?

0
source share
2 answers

Try specifying the tag for the button as the row number of the table. Then check the tag in the button click method, and then follow the logic to change the image.

-(void)changeMapType:(id)sender
{
   UIButton *button = (UIButton *)sender;
   // Check tag of button
   // Some code
   //Change image after checking
   [button setImage:yourImage forState:UIControlStateNormal];

}
+1
source

, cellForRowAtIndex. , . .

0

Source: https://habr.com/ru/post/1784745/


All Articles