滚动后检查图像移除。
我必须重用cell并且也不想使用 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;
}
}
答案 0 :(得分:0)
嗯,实现的问题是,你只在buttonClicked方法中设置了imageView.hidden属性。
当一个单元格没有在屏幕上显示片刻然后再次显示时,再次调用cellForRow。
这里的问题是,您不存储imageView.hidden已设置为NO的单元格。这就是为什么每次再次显示一个单元格时,cellForRow:将其imageView.hidden设置为YES。
因此解决方案是存储单元格的编号,其中imageView.hidden = NO并检查cellForRow,如果该索引路径的单元格是imageView.hidden = NO。
答案 1 :(得分: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];
}