通过按钮、标签和单元格重用问题检查 uitableviewcell 中的图像无法正常工作
Posted
技术标签:
【中文标题】通过按钮、标签和单元格重用问题检查 uitableviewcell 中的图像无法正常工作【英文标题】:Check image not working properly in uitableviewcell over button, tag and cell reuse issue 【发布时间】:2013-05-03 10:37:34 【问题描述】:检查滚动后删除的图像。 我必须重用单元格,也不想使用 didselectrow 方法,因为我在一个单元格中有多个按钮,这是我为 *** 创建的示例。
我在google上搜索了很多,发现很无奈。
这是我的示例代码
//
// 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;
【问题讨论】:
【参考方案1】:嗯,你实现的问题是,你只在 buttonClicked 方法中设置了 imageView.hidden 属性。
当一个单元格在屏幕上暂时未显示然后再次显示时,将再次调用 cellForRow。
这里的问题是,您没有将 imageView.hidden 设置为 NO 存储在哪个单元格中。这就是为什么每次再次显示单元格时,cellForRow: 都会将其 imageView.hidden 设置为 YES。
因此解决方案是存储单元格的编号,其中 imageView.hidden= NO 并检查 cellForRow 如果该索引路径中的单元格是 imageView.hidden= NO 的单元格。
【讨论】:
【参考方案2】:为搜索相同功能的人回答我自己的问题,这里我是如何进行更改的
- (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];
【讨论】:
以上是关于通过按钮、标签和单元格重用问题检查 uitableviewcell 中的图像无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
目标 C:如果将标签添加到单元格,则 UITable 方法“didSelectRowAtIndexPath”不起作用