简体   繁体   English

如何通过iPhone中的切换按钮在单元格上设置图像

[英]how to set image on cell through togglebutton in iphone

Hi I try this code but it not show image on cell where i am wrong i want when i click every cell then i want to display image or when i scroll cell then image not disappear it should be appear on that cell i use this code but it not working pease help me on this. 嗨,我尝试此代码,但它不显示单元格上的图像我错了,我想当我单击每个单元格然后我要显示图像,或者当我滚动单元格然后图像不消失时,它应该出现在该单元格上,但我使用此代码它不能正常工作请帮助我。


@interface imageclass : UITableViewController
{

    BOOL changeimagetype;
    //UIImage *btnImage;
    UIButton *mimageButton;
    UIImageView *onButtonView;

}

@property (nonatomic,retain)UIImageView *onButtonView;
@property BOOL changeimagetype;
@property (nonatomic,retain)UIButton *mimageButton;

-(void)changeMapType:(id)sender;
@end

#import "imageclass.h"


@implementation imageclass
@synthesize onButtonView,changeimagetype,mimageButton;



-(void)changeMapType:(id)sender
{
    changeimagetype =!changeimagetype;
    if(changeimagetype == YES)
    {
        //[typechange setImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
        //self.myGreatMapView.mapType = MKMapTypeStandard;
        onButtonView.image = [UIImage imageNamed:@"alarm_OF.png"];
        [mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
        //someBarButtonItem.image = [UIImage imageNamed:@"alarm_ON..png"];
        //changeimagetype =NO;
    }
    else
    {
        //self.myGreatMapView.mapType = MKMapTypeSatellite;     
        onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
        [mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
    }

}


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

Define you tableview delegate like this. 像这样定义您的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;
}

I tried this different approach, and it worked. 我尝试了这种不同的方法,并且奏效了。 While configuring the button, 在配置按钮时,

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

} }

And in changeMapType: method, 在changeMapType:方法中,

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

Here is the code:- Add this in cellForRowAtIndexPath method. 下面是代码:-在cellForRowAtIndexPath方法中添加此代码。 And also set flag variable in your target method for togglebutton. 并在目标方法中为切换按钮设置标志变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM