gpt4 book ai didi

ios - 添加手势识别器以指定表格中单元格的对象

转载 作者:行者123 更新时间:2023-11-29 02:05:21 25 4
gpt4 key购买 nike

我正在尝试向所有单元格中的 uiimageview 添加手势识别器,这将使该索引路径上的 uiimageview 更改图像,但我无法弄清楚如何告诉手势 ibaction 更改该索引路径中的图像。

我用这段代码实现的是它只在最后一个单元格中工作正常,所有其他单元格也没有得到手势。

我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
imgConfirm = (UIImageView *)[cell viewWithTag:107];
[imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
[imgConfirm addGestureRecognizer:self.tapGestureM2];
return cell;
}

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender {
NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image);
NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]);

if ([imgConfirmData1 isEqualToData:imgConfirmData2]) {
[imgConfirm setImage:[UIImage imageNamed:@"icon"]];
}
else{
[imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
}
}

如果之前有人问过这个问题,我很抱歉 :) 但我搜索了几个小时,但找不到合适的时间。

*编辑:在这段代码中,每个单元格都有点击识别器,但 ibaction 没有被触发

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
indexpath=[self.tableViewM2 indexPathForCell:cell];
// Configure the cell...
UIImageView *imgConfirm = (UIImageView *)[cell viewWithTag:107];
[imgConfirm setImage:[UIImage imageNamed:@"icon2"]];

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init];
// setup gesture as needed
[imgConfirm addGestureRecognizer:gesture];
return cell;
}

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender {
NSLog(@"%d,%d",indexpath.row,indexpath.section);
UIImageView *imgConfirm = (UIImageView *)sender.view;
NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image);
NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]);

if ([imgConfirmData1 isEqualToData:imgConfirmData2]) {
[imgConfirm setImage:[UIImage imageNamed:@"icon"]];
}
else{
[imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
}
}

*Edit2:我终于找到了怎么做,下面的代码是正确的,但需要告诉手势触发 Action !

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTap:)];

最佳答案

您应该从手势中获取 ImageView 。 imgConfirm 不需要使用实例变量。您还需要为每个 ImageView 创建一个单独的手势识别器。您不能一遍又一遍地重复使用同一个。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
UIImageView *imgConfirm = (UIImageView *)[cell viewWithTag:107];
[imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] inittWithTarget:self action:@selector(tapGestureTap:)];
// setup gesture as needed
[imgConfirm addGestureRecognizer:gesture];
return cell;
}

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender {
UIImageView *imgConfirm = (UIImageView *)sender.view;
NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image);
NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]);

if ([imgConfirmData1 isEqualToData:imgConfirmData2]) {
[imgConfirm setImage:[UIImage imageNamed:@"icon"]];
}
else{
[imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
}
}

这里缺少的一个重要部分是,如果用户滚动表格,图像将被重置。您需要添加更多代码来跟踪每个图像的当前状态,以便您的 cellForRowAtIndexPath: 设置正确的图像。

关于ios - 添加手势识别器以指定表格中单元格的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838407/

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