0

检查滚动后删除的图像。
我必须重用单元格,也不想使用didselectrow方法,因为我在一个单元格中有多个按钮,这是我为 stackoverflow 创建的示例。

表格视图的屏幕截图 我在谷歌搜索了很多,发现无能为力。

这是我的示例代码

//
//  TVViewController.h
//  tableviewcheck



#import <UIKit/UIKit.h>

@interface TVViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

    UITableView *sampleTableView;
    UIImageView * imageView1;
    UIButton* aButton1;
    UIImageView *imageViewchk1;
    int tag;

}

@property (nonatomic, strong) IBOutlet UITableView *sampleTableView;

@end


//  TVViewController.m
//  tableviewcheck

#import "TVViewController.h"

@interface TVViewController ()

@end

@implementation TVViewController
@synthesize sampleTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Return the number of rows in the section.
    // Usually the number of items in your array (the one that holds your list)
    //  NSLog(@"selected %i and deliveryArray %i",[queueArray count],[deliveryArray count]);
                return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Where we configure the cell in each row

    // NSString *CellIdentifier =[NSString stringWithFormat:@"%i-%i", indexPath.section,indexPath.row];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    }
        imageViewchk1 = [[UIImageView alloc] initWithFrame:CGRectMake(56,0 , 24,24)];
        imageViewchk1.image = [UIImage imageNamed:@"check.png"];
        imageViewchk1.tag=indexPath.row+10000;
          imageViewchk1.hidden=YES;

        imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80,80)];
        imageView1.image = [UIImage imageNamed:@"newframe.png"];

        aButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton1 setTag:indexPath.row];
        [aButton1 setImage:imageView1.image forState:UIControlStateNormal];
        aButton1.frame = CGRectMake(0, 0, 80,80);
        [aButton1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [aButton1 addSubview:imageViewchk1];
        [cell.contentView addSubview:aButton1];

    return cell;

       }


-(void)buttonClicked:(UIButton*)sender {
    tag = sender.tag;
    NSLog(@"buttonClicked %i",tag);

    UIImageView *imageView = (UIImageView *)[sampleTableView viewWithTag:tag+10000];


        if(imageView.hidden)
        {
            imageView.hidden=NO;
        }
        else {
            imageView.hidden=YES;

        }
    }
4

2 回答 2

0

Well, the problem with you implementation is, that you only set the imageView.hidden property in the buttonClicked method.

When a cell is not shown on the screen for a moment and then shown again, cellForRow is called again.

The problem here is, that you do not store in which cell the imageView.hidden has been set to NO. That is why every time a cell is shown again, the cellForRow: sets its imageView.hidden to YES.

So solution will be to store the numbers of the cell, where imageView.hidden= NO and check in cellForRow if the cell at that indexpath is one where imageView.hidden= NO.

于 2013-05-03T10:47:18.803 回答
0

为搜索相同功能的人回答我自己的问题,在这里我是如何进行更改的

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    tempArray=[[NSMutableArray alloc] init];
    for(int count=0;count<50;count++)
        [tempArray addObject:[NSNumber numberWithBool:NO]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Return the number of rows in the section.
    // Usually the number of items in your array (the one that holds your list)
    //  NSLog(@"selected %i and deliveryArray %i",[queueArray count],[deliveryArray count]);
                return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];



    }

    imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80,80)];
    imageView1.image = [UIImage imageNamed:@"newframe.png"];

    imageViewchk1 = [[UIImageView alloc] initWithFrame:CGRectMake(56,0 , 24,24)];
    imageViewchk1.image = [UIImage imageNamed:@"check.png"];
    imageViewchk1.tag=indexPath.row+10000;
    imageViewchk1.hidden=YES;


    aButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [aButton1 setTag:indexPath.row];
    [aButton1 setImage:imageView1.image forState:UIControlStateNormal];
    aButton1.frame = CGRectMake(0, 0, 80,80);
    [aButton1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [aButton1 addSubview:imageViewchk1];


   BOOL selected=[[tempArray objectAtIndex:indexPath.row] boolValue];
    if(selected)
    {
      //  imageViewchk1.image = [UIImage imageNamed:@"check.png"];

        imageViewchk1.hidden=NO;
    }
    else
    {
      imageViewchk1.hidden=YES;
    }

      [cell.contentView addSubview:aButton1];

    return cell;

       }



-(void)buttonClicked:(UIButton*)sender {
    tag = sender.tag;
    NSLog(@"buttonClicked %i",tag);


     NSIndexPath *index=[sampleTableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
     BOOL selected=[[tempArray objectAtIndex:index.row] boolValue];
    if(selected)
    {
     [tempArray replaceObjectAtIndex:index.row withObject:[NSNumber numberWithBool:NO]];
    }
    else
    [tempArray replaceObjectAtIndex:index.row withObject:[NSNumber numberWithBool:YES]];

    [sampleTableView reloadData];


    }
于 2013-05-07T11:01:35.367 回答