如何在iphone中通过切换按钮在手机上设置图像

如何在iphone中通过切换按钮在手机上设置图像,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,嗨,我尝试了这段代码,但它没有在单元格上显示图像我错了,我想当我点击每个单元格时,我想显示图像,或者当我滚动单元格时,图像不会消失,它应该出现在该单元格上。我使用这段代码,但它不起作用。请帮助我 我尝试了这种不同的方法,它奏效了。 在配置按钮时 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *Ce

嗨,我尝试了这段代码,但它没有在单元格上显示图像我错了,我想当我点击每个单元格时,我想显示图像,或者当我滚动单元格时,图像不会消失,它应该出现在该单元格上。我使用这段代码,但它不起作用。请帮助我





我尝试了这种不同的方法,它奏效了。 在配置按钮时

 - (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, 30, 50);
mimageButton.tag = 1;               
[tempButton setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];
[tempButton setImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateSelected];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:@selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];

}
// Configure the cell...

return cell;
}

在changeMapType:method中

-(void)changeMapType:(id)sender{
    changeimagetype =!changeimagetype;
    sender.selected = changeimagetype;
 }

像这样定义tableview委托

- (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, 30, 50);
    mimageButton.tag = 1;               

    onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
    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];
    [onButtonView release];
    }
    // Configure the cell...

    return cell;
}
代码如下:- 在cellForRowAtIndexPath方法中添加此项。并在目标方法中为togglebutton设置标志变量

 if(flagButton)
{
    onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
}
else
{
     onButtonView.image = [UIImage imageNamed:@"alarm_OFF.png"];
}

你到底想做什么,请详细解释。@iPhoneDeveloper我想在单元格上显示图像默认情况下,当我单击位于单元格上的按钮时,on.png是show on按钮,然后图像应更改为off.png,但它不能正常工作。如果我滚动tableview单元格,则我的图像会消失,它不稳定,该位置如何解决它帮助我打开本主题介绍如何在tableview委托中设置按钮的背景图像,以及如何设置按钮图像changeMapType操作。您想做什么?您需要将切换按钮的值保存在BOOL变量中,因为每当tableview重新加载按钮时,您的按钮会再次初始化,并且图像会发生更改。。您正在使用tableview。请在更改后重新加载tableview。我想在单击时更改图像我是单元格中的创建按钮不在视图中它是如何工作的请解释方法您为按钮目标编写的代码是正确的只是您需要在方法中设置flagButton是或否changeMapType:另外,您需要在单元格中添加上述代码ForInExpath您能给出一些吗这是示例代码。在cellForRowAtIndexPath中,创建mimageButton后,在“mimageButton.tag=1;”行之后,用我的答案中的前三行替换下四行。并将您的changeMapType:方法替换为我提供的方法。在此方法中,我得到错误-(void)changeMapType:(id)sender{changeimagetype=!changeimagetype;sender.selected=changeimagetype;//这里我得到了错误/Users/pradeepyadav/Desktop/tableimage/Classes/imageclass.m:59:0/Users/pradeepyadav/Desktop/tableimage/Classes/imageclass.m:59:error:request for member'selected'在非结构或联合中}将方法签名更改为,-(void)CuxeMaPoTo:(UBIUTT*)发送者,或类型化发送者到UIButtonshanti kamichetty的指针,我尝试作为Par UR代码,但是现在我的图像不显示在它的工作单元上,请帮助我的单元格显示我的空白。
- (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, 30, 50);
    mimageButton.tag = 1;               

    onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
    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];
    [onButtonView release];
    }
    // Configure the cell...

    return cell;
}
 if(flagButton)
{
    onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
}
else
{
     onButtonView.image = [UIImage imageNamed:@"alarm_OFF.png"];
}