如何通过按行来调用警报中的表行索引

时间:2013-07-09 23:12:11

标签: ios tableview uialertview

我有一个表格视图。当用户选择一行时,我想显示一个标准“你确定吗?”对话。但是如果他们确定一些关于做什么的信息与他们选择的行有关。我怎样才能访问它?

这是我到目前为止所拥有的。

- (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];
        }
    }
}

任何提示将不胜感激。感谢。

2 个答案:

答案 0 :(得分: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];
}

希望这有帮助!

答案 1 :(得分:2)

看起来你只需要跟踪UIAlertView带来的表的特定行。

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

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

因此,在您的“didSelectRowAtIndexPath”方法中,请在创建提醒后执行此操作:

alert.tag = indexPath.row;

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

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

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