gpt4 book ai didi

ios - 如果用户从 UIAlertView 选择取消按钮,则停止从 UITable 中删除用户

转载 作者:行者123 更新时间:2023-11-29 02:57:07 28 4
gpt4 key购买 nike

我有一个表,其中包含从 plist 加载的一些名称,我想做的基本上是删除用户选择的那个,一切正常,但我想做的是我无法弄清楚基本上是为用户提供取消或使用 UIAlertView 继续删除的选项。

这就是我所拥有的,但显然不起作用,无论触摸哪个按钮,它都会删除用户。

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// This is fine since here I'm only notifying the user that guest cannot be deleted
UITableViewCell *celectedRow = [self.tableScores cellForRowAtIndexPath:indexPath];
NSString *removeUserWithKey = celectedRow.textLabel.text;

if ([removeUserWithKey isEqual: @"Guest"])
{
// don't remove anything and show message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information!"
message:@"The user 'Guest' cannot be removed."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];

}
else
{
// the problem I have is here, how can I stop deleting and saving
// if user selects the cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information!"
message:@"The user 'Guest' cannot be removed."
delegate:self
cancelButtonTitle:@"Continue"
otherButtonTitles:@"Cancel", nil];
[alert show];

//IF USER NAME IS OTHER THAN GUEST REMOVE IT
// remove selected row from table
[arrayRecords removeObjectAtIndex:indexPath.row];

// remove selected user from dictionary
[dictionaryCopyOfRecordsFromPlist removeObjectForKey:removeUserWithKey];

// write dictionary to plist after removing items
[self.pListReader writeToPlist:@"studentsRecords.plist" withDictionary:dictionaryCopyOfRecordsFromPlist];

// reload items in table to reflect any changes made
[self.tableScores reloadData];
}
}



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

if([title isEqualToString:@"Continue"])
{
NSLog(@"Continue.");
}
else if([title isEqualToString:@"Cancel"])
{
NSLog(@"Cancelled");
}
}

同样,如果我不想为用户提供取消删除的选项,此代码也能正常工作。

如果用户选择取消按钮,我如何构建我的代码以取消删除过程?

有什么建议吗?

最佳答案

您需要确保仅当用户按下继续按钮时才从数组中删除对象。将移除访客的代码移动到警报 View 委托(delegate)

用行号标记警报 View 。

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information!"
message:@"The user 'Guest' cannot be removed."
delegate:self
cancelButtonTitle:@"Continue"
otherButtonTitles:@"Cancel", nil];
alert.tag = indexPath.row;
[alert show];

删除 Alert View 委托(delegate)中的记录。

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

if([title isEqualToString:@"Continue"])
{
NSLog(@"Continue.");

//Remove the corresponding row.
[arrayRecords removeObjectAtIndex:alertView.tag];

// remove selected user from dictionary
[dictionaryCopyOfRecordsFromPlist removeObjectForKey:removeUserWithKey];

// write dictionary to plist after removing items
[self.pListReader writeToPlist:@"studentsRecords.plist" withDictionary:dictionaryCopyOfRecordsFromPlist];

// reload items in table to reflect any changes made
[self.tableScores reloadData];
}
else if([title isEqualToString:@"Cancel"])
{
NSLog(@"Cancelled");
//Do Nothing
}
}

关于ios - 如果用户从 UIAlertView 选择取消按钮,则停止从 UITable 中删除用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23749470/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com