0

I have a table view. When a user selects a row, I would like to display a standard "Are you sure?" sort of dialog. But if they are sure some info about what to do is tied to the row they selected. How can I get access to that?

Here's what I have so far.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SitePeople *sitePersonAtIndex = [self.sitePeoples objectAtIndex:indexPath.row];
    UIAlertView *alert;
    alert = [[UIAlertView alloc] initWithTitle:@"Are you sure?" message:[NSString stringWithFormat:@"This will send an email to %@", sitePersonAtIndex.SitePerson] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Send", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    self.alertShowing = NO;
    if (buttonIndex == 0)
    {
        //cancelled, do nothing
    } else {
        //SitePeople *sitePersonAtIndex = [self.sitePeoples objectAtIndex:indexPath.row];
        SitePeople *sitePersonAtIndex = [self.sitePeoples objectAtIndex:[self.tableView indexPathForSelectedRow]];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *email = [defaults stringForKey:@"email"];
        NSString *password = [defaults stringForKey:@"password"];
        [self sendEmail:[NSString stringWithFormat:@"http://service.pharmatech.com/Share/emailstudy/%@/%@/%@/%@", email, password, self.study.ProjectID, sitePersonAtIndex.SitePeopleID]];
        UIAlertView *alert;
        if (self.alertShowing == NO)
        {
            if ([self.sendEmailResult.WasSuccessful isEqual: @"true"]) {
                alert = [[UIAlertView alloc] initWithTitle:@"Success" message:self.sendEmailResult.Message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            } else {
                alert = [[UIAlertView alloc] initWithTitle:@"Error" message:self.sendEmailResult.Message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            }
            [alert show];
        }
    }
}

Any tips would be appreciated. Thanks.

4

2 回答 2

2

当用户选择一个单元格时,您可以有一个实例变量来保存索引路径(作为一种记住它的方式),然后,当警报视图被关闭时,您可以使用该实例变量来检索您需要的信息。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.myIndexPath = indexPath; // remember the index path that was selected
    SitePeople *sitePersonAtIndex = [self.sitePeoples objectAtIndex:indexPath.row];
    UIAlertView *alert;
    alert = [[UIAlertView alloc] initWithTitle:@"Are you sure?" message:[NSString stringWithFormat:@"This will send an email to %@", sitePersonAtIndex.SitePerson] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Send", nil];
    [alert show];
}

希望这可以帮助!

于 2013-07-09T23:22:48.153 回答
2

看起来您只需要跟踪显示您的 UIAlertView 的表的特定行。

有几种方法可以解决这个问题。

一种方法可能是简单地使用您的 UIAlertView 的“ tag”属性来临时存储行号。

因此,在您的 " didSelectRowAtIndexPath" 方法中,在创建警报后执行此操作:

alert.tag = indexPath.row;

在您的 " didDismissWithButtonIndex" 委托方法中,您可以通过以下方式从标签中获取行:

NSInteger rowOfTable = alertView.tag;
SitePeople *sitePersonAtIndex = [self.sitePeoples objectAtIndex:rowOfTable];

另一种方式可能是@LuisCien 的......并向他 +1!

于 2013-07-09T23:22:55.150 回答