Atma
Atma

Reputation: 29767

how to change cell from didselectrowatindexpath based on alertview selection

I have an action that takes place in my tableview at didselectrowatindexpath if a user clicks in the affirmative on an alertview that pops up. I want to change the text in a proprty of the cell based on if the user clicks Yet on the alertview.

my code looks like:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    InviteTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    Invite *invite = [self.inviteContactsArray objectAtIndex:indexPath.row];
    _selectedRow = indexPath;

    NSString *message = [NSString stringWithFormat:@"Are you sure you want to send an email invite ?"];
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Confirm Invite" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

    [alert show];
}

The alert then does the invite, but I want it to also change a label on the cell to "invited". I tried this, but it did not work.

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSLog(@"fire invite");
        [self invite];
        InviteTableViewCell *cell = [self.tableView cellForRowAtIndexPath:_selectedRow];
        cell.inviteLabel.text = @"invited";
        [self.tableView reloadRowsAtIndexPaths:@[_selectedRow] withRowAnimation:UITableViewRowAnimationAutomatic];

    }

}

How can I have the cell changed after the user chooses yet to invite on the alertview?

Upvotes: 0

Views: 1549

Answers (3)

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

You are changing a cell which is being thrown away!

cell.inviteLabel.text = @"invited"; // <-- This cell will no longer exist after next call.
[self.tableView reloadRowsAtIndexPaths:@[_selectedRow] withRowAnimation:UITableViewRowAnimationAutomatic];

You need to update your model, then when the cell is reloaded the model will use the new value for the cell.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSLog(@"fire invite");
        [self invite];

        // However you need to record this update to the data model
        MyObject *object = self.objects[indexPath];
        object.invited = YES;

        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    …

    // Now you use the data model to provide the correct value for the reloaded cell.
    MyObject *object = self.objects[indexPath];
    if (object.invited)
        cell.inviteLabel.text = @"invited";

    …
}

Upvotes: 2

Benpaper
Benpaper

Reputation: 144

[self.tableView indexPathsForSelectedRows] keep track of the indexPath of the selected row.

Upvotes: 0

Brandon
Brandon

Reputation: 2427

in your alertview handler updated your model data so that cellForRowAtIndexPath will load the cell correctly then reload the cell with this method:

[self.tableview reloadRowsAtIndexPaths:@[_selectedRow] withRowAnimation:UITableViewRowAnimationAutomatic];

Upvotes: 0

Related Questions