Ios 更改表格视图单元格';UIS图像点击
可能是一个简单的解决办法 我的table view控制器的每个单元格中都有一个图像,上面有一个点击手势识别器。这工作很好,而且日志记录也很恰当 我需要的是将图像从默认状态更改为在“选定”状态(绿色)和“取消选定”状态(红色)之间切换。换句话说,它是一个灰色的检查表,然后你点击图像,图像变成绿色的复选标记,再次点击,它变成红色的x 这里有一个陷阱:当这只是didSelectRowAtIndexPath方法中的一个条件语句时,它就起作用了,但是由于我添加了手势识别器并创建了Ios 更改表格视图单元格';UIS图像点击,ios,objective-c,uitableview,uiimage,uitapgesturerecognizer,Ios,Objective C,Uitableview,Uiimage,Uitapgesturerecognizer,可能是一个简单的解决办法 我的table view控制器的每个单元格中都有一个图像,上面有一个点击手势识别器。这工作很好,而且日志记录也很恰当 我需要的是将图像从默认状态更改为在“选定”状态(绿色)和“取消选定”状态(红色)之间切换。换句话说,它是一个灰色的检查表,然后你点击图像,图像变成绿色的复选标记,再次点击,它变成红色的x 这里有一个陷阱:当这只是didSelectRowAtIndexPath方法中的一个条件语句时,它就起作用了,但是由于我添加了手势识别器并创建了imageTapped方法
imageTapped
方法,我似乎无法将其翻译过来。因此,其他有用的线程,如不适合我
一如既往地谢谢你。你们是最好的
代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PlacesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create ImageView
cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
cell.imageView.userInteractionEnabled = YES;
[cell addSubview:cell.imageView];
return cell;
}
//Method controlling what happens when cell's UIImage is tapped
-(void)imageTapped
{
UITableViewCell *cell;
UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"];
UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];
if (cell.imageView.image == imageDefault) {
cell.imageView.image = imageGreen;
cell.selected = true;
NSLog(@"Selected");
} else {
cell.imageView.image = imageRed;
cell.selected = false;
NSLog(@"Deselected");
}
}
我已经修改了你的代码,请检查
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PlacesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create ImageView
cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
cell.imageView.userInteractionEnabled = YES;
//[cell addSubview:cell.imageView];
return cell;
}
//Method controlling what happens when cell's UIImage is tapped
-(void)imageTapped:(UIGestureRecognizer*)gesture
{
UIImageView *selectedImageView=(UIImageView*)[gesture view];
UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"];
UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];
if (selectedImageView.image == imageDefault) {
selectedImageView.image = imageGreen;
//cell.selected = true;
NSLog(@"Selected");
} else {
selectedImageView.image = imageRed;
//cell.selected = false;
NSLog(@"Deselected");
}
}
我已经修改了你的代码,请检查
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PlacesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PlacesCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create ImageView
cell.imageView.image = [UIImage imageNamed:@"checkmark.png"];
//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
cell.imageView.userInteractionEnabled = YES;
//[cell addSubview:cell.imageView];
return cell;
}
//Method controlling what happens when cell's UIImage is tapped
-(void)imageTapped:(UIGestureRecognizer*)gesture
{
UIImageView *selectedImageView=(UIImageView*)[gesture view];
UIImage *imageDefault = [UIImage imageNamed:@"checkmark.png"];
UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];
if (selectedImageView.image == imageDefault) {
selectedImageView.image = imageGreen;
//cell.selected = true;
NSLog(@"Selected");
} else {
selectedImageView.image = imageRed;
//cell.selected = false;
NSLog(@"Deselected");
}
}
很好用!谢谢@pawan!很好用!谢谢@pawan!