如何在iPhone的每一个tableviewcell上处理工具按钮



我的代码工作良好,但它只适用于单个单元格。当我定义5行,那么它只适用于最后一个单元格。如果我点击1单元格,那么值displayimage上的最后一个单元格只有它不显示我在哪里点击,我是哪个单元格点击如何处理工具按钮更改图像的每个单元格点击按钮图像此代码。

-(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];
    }

你能帮我修改代码吗?

尝试将按钮的标记作为表的行号。然后检查按钮按下方法中的标签,然后执行更改图像的逻辑。

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

取一个数组,将选中的按钮标签存储在数组中,同时在cellForRowAtIndex方法中显示按钮。检查按钮标签是否在那个数组中。

最新更新