gpt4 book ai didi

ios - 点击时更改 TableView 单元格的 UIImage

转载 作者:行者123 更新时间:2023-11-28 19:04:17 25 4
gpt4 key购买 nike

可能是一个简单的修复。

我的表格 View Controller 的每个单元格内都有一个图像,上面有一个点击手势识别器。这工作正常并且记录正确。

我需要的是将图像从其默认状态更改为然后在“选定”状态(绿色)和“取消选定”状态(红色)之间切换。换句话说,它是一个一开始是灰色的 list ,然后你点击图像,图像变成绿色的勾号,再次点击,它变成红色的 x。

关键在于:当它只是 didSelectRowAtIndexPath 方法中的一个条件语句时,它是否有效,但由于我添加了手势识别器并创建了 imageTapped 方法,我似乎无法翻译过来。因此,其他有用的线程,如 this不要为我工作。

一如既往的感谢。你们是最棒的。

代码如下:

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

关于ios - 点击时更改 TableView 单元格的 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21838721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com