我如何测试某个表格视图单元格是否具有特定图像?

发布于 2024-12-09 00:04:01 字数 815 浏览 0 评论 0原文

在我的应用程序中,您可以添加和删除单元格。我只希望用户能够为每个单元格添加 1 个。那么,有没有办法测试一个细胞是否已经拥有某个图像呢?

喜欢:

if(myTableView.cell.imageView == @"image.png"){
// do something
}

请帮忙!谢谢

编辑

此按钮允许我们将单元格和图像添加到新单元格中。

  - (IBAction)outlet1:(id)sender {
if (cart.cell.tag == 1) {
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"You have already added this" message:@"Go to My Cart and add more if you like" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
else {
[cart.cells addObject:@"1"];
 [[cart.cells lastObject] setTag:1];
UIImage * myImage = [UIImage imageNamed:@"paddle1.png"];
[cart.imageArray addObject:myImage];

} 所以

我基本上想测试这个单元格是否已经被添加!

In my app, you can add and delete cells. I only want the user to be able to add 1 of each cell. So, is there a way to test if a cell already has a certain image?

like:

if(myTableView.cell.imageView == @"image.png"){
// do something
}

Please help! THanks

EDIT

This button allows us to add the cells and the images to the new cells.

  - (IBAction)outlet1:(id)sender {
if (cart.cell.tag == 1) {
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"You have already added this" message:@"Go to My Cart and add more if you like" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
else {
[cart.cells addObject:@"1"];
 [[cart.cells lastObject] setTag:1];
UIImage * myImage = [UIImage imageNamed:@"paddle1.png"];
[cart.imageArray addObject:myImage];

}
}

so i basically want to test if this cell has been added already!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荆棘i 2024-12-16 00:04:01

如果您只有几个不同的图像,则不需要检查图像,而是应该使用以下命令为与图像对应的单元格设置标签号
[单元格设置标签:(int)];所以 paddle2.png 可以是 2, paddle1.png 可以是 1,你可以 [cell setTag:1] 表示 paddle1.png 。比测试时简单地检查标签:

If ([cell tag] == 1) {
//做某事
这是您

的按钮的代码:

   - (IBAction)outlet2:(id)sender {
[cart.cells addObject:@"1"];
[[cart.cells lastObject] setTag:2];
UIImage * myImage = [UIImage imageNamed:@"paddle2.png"];
[cart.imageArray addObject:myImage];
}

这是测试代码:

if(myTableView.cell.tag == 2){
// do something
  }

If you only have a few different images than you don't need to check for an image, rather you should set a tag number to the cell that corresponds to an image using
[cell setTag:(int)]; so paddle2.png can be 2 and paddle1.png can be 1 and you would [cell setTag:1] for paddle1.png . Than when testing simply check the tag:

If ([cell tag] == 1) {
//do something
}

here is the code for your button:

   - (IBAction)outlet2:(id)sender {
[cart.cells addObject:@"1"];
[[cart.cells lastObject] setTag:2];
UIImage * myImage = [UIImage imageNamed:@"paddle2.png"];
[cart.imageArray addObject:myImage];
}

and here is the test code:

if(myTableView.cell.tag == 2){
// do something
  }
心房敞 2024-12-16 00:04:01

据我所知,您无法检查图像名称,但您可以保留存储在单元格中的图像名称数组。然后,您可以检查行的索引和图像数组名称的索引,看看它是否是您要查找的内容:

if ([[[myimages objectAtIndex:i] valueForKey:@"ImageName"] isEqualToString:@"image.png"])
{
   // you got what you were looking for,
}

上面的“i”将是indexPath.row值 - 我假设您在委托方法 didSelectRowAtIndexPath。

As far as I know you cannot check for an image name, but you can keep an array of the images names that are stored in the cells. Then you can check the index of the row and the index of the image array names to see if it is what you are looking for:

if ([[[myimages objectAtIndex:i] valueForKey:@"ImageName"] isEqualToString:@"image.png"])
{
   // you got what you were looking for,
}

'i' above would be the indexPath.row value - I am assuming that you are check this on the delegate method didSelectRowAtIndexPath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文