Ben
Ben

Reputation: 1031

How to get the indexpath?

I am trying to find the IndexPath of an UITableViewCell when I clicked a button, it doesn´t look easy because I am using also an UIAlert. This my code I am using :

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";


    UpdatesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UpdatesTableViewCell" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (UpdatesTableViewCell*)view;
            }
        }
    }


    cell.textLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"Title"];
    cell.detailTextLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"Description"];

    //////////This button is the one sending the action////////////////
    [cell.deletebutton addTarget:self action:@selector(deletetherow:) forControlEvents:UIControlEventTouchUpInside];


    imagepath = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"Photo"];

    if([imagepath isEqualToString:@"(null)"])
    {
        cell.imageView.image = [UIImage imageNamed:@"noimage.png"];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        cell.imageView.clipsToBounds = YES; 

    }
    else {

        cell.imageView.image = [[[UIImage alloc] initWithContentsOfFile:imagepath] autorelease];

    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

- (void)deletetherow:(id)sender{

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Title"
                                                  message:@"Delete it ?"
                                                 delegate:self
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"delete", nil];
[message show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

if([title isEqualToString:@"Eliminar"])
{

        NSLog(@"OFFLINE");
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        
        NSString *documentsDirectory = [paths objectAtIndex:0];        
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"datatesting.plist"];
        libraryContent = [[NSMutableArray alloc] initWithContentsOfFile:path];
        [libraryContent removeObjectAtIndex:0]; ///// Here instead 0, I want to put the indexpath to know what row content to delete.
        NSLog(@"delete the Row:%@",libraryContent);
        [libraryContent writeToFile:path atomically:YES];

        dao = [[Dfetch alloc] initWithLibraryName:@"Diario"];
        [self.tableviewnew reloadData];

    }
  }
}

So my question is, how can get the indexpath.row inside the alertView method ?

Upvotes: 0

Views: 365

Answers (2)

Matthias
Matthias

Reputation: 8180

I'm not sure if I get your proplem, but for passing a value to a method, you have a number of possibilities:

  • Declare an instance variable, assign the value, and use it in the method
  • Declare a global variable (bad idea!), assign the value, and use it in the method
  • Use it as a parameter for your method (works only if the method signature is not enforced, e.g. by a protocol)

Upvotes: 0

Totumus Maximus
Totumus Maximus

Reputation: 7583

You can give the UIButton a tag and tag it with the indexPath.row, then when you press the button just retrieve it from the (id)sender object and then retrieve the tag from there.

Upvotes: 3

Related Questions