如何处理iPhone中每一个tableviewcell上的toogle按钮 [英] how to handle toogle button on every and each on tableviewcell in iPhone

查看:49
本文介绍了如何处理iPhone中每一个tableviewcell上的toogle按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码工作正常,但仅适用于单个单元格.当我定义5行时,它仅适用于最后一个单元格.如果我单击1个单元格,则仅在最后一个单元格上的值displayimage不会显示我单击的位置以及我单击的单元格的处理方式对于每个单元格单击此按钮,都会处理按钮切换图像.

My code is working fine but it works only for a single cell. When i define 5 rows then it works only for the last cell. If I click on 1 cell then value displayimage on last cell only it not display where i am click and which cell i am click how to handle toogle button change image for each and every cell click on button image 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];

    }

您能帮我更正代码吗?

推荐答案

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

Try giving the tag for the button as the row number of the table. Then check the tag in the button press method and then perform the logic to change image.

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

}

这篇关于如何处理iPhone中每一个tableviewcell上的toogle按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆