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



嗨,我尝试这个代码,但它不显示图像上的单元格,我错了,我想当我点击每个单元格,然后我要显示图像,或者当我滚动单元格,然后图像不消失,它应该出现在那个单元格上,我使用这个代码,但它不工作,请帮助我。


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

像这样定义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的目标方法中设置flag变量。

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

我尝试了这种不同的方法,而且很有效。在配置按钮时,

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

最新更新