iProRage
iProRage

Reputation: 424

How can i test if a certain table view cell has a certain image?

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!

Upvotes: 0

Views: 270

Answers (2)

Zigglzworth
Zigglzworth

Reputation: 6803

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
  }

Upvotes: 1

Slee
Slee

Reputation: 28248

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.

Upvotes: 0

Related Questions