当用户点击delete
编辑模式中的默认UITableView
按钮时,用户应该获得一个警报视图,如果他在AlertView中再次点击删除,该行应该被删除。最初我已经完成了使用以下没有AlertView的代码,它工作正常。
[[self categoriesArray]removeObjectAtIndex:[indexPath row]];
NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
[self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];
但是现在,因为我必须在alertview委托方法中使用相同的代码。我不知道如何获得[indexPath row]
答案 0 :(得分:4)
将索引路径设置为UIAlertView
标记,然后从Delegate获取。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath // indexPath is here
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Are you sure want to delete" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
[alertView setTag:indexPath.row]; // Assigning here.
[alertView show];
}
}
// UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%d",alertView.tag); // Your Indexpath is here
**Edited:**
NSIndexPath * path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
[[self categoriesArray]removeObjectAtIndex:[path row]];
NSArray * indexPathsToRemove = [NSArray arrayWithObject:path];
[self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
[self.categoriesArray writeToFile:[self dataFilePath] atomically:YES];
}
答案 1 :(得分:2)
将标记设置为commitEditingStyle方法的警报视图
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath // indexPath is here
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Delete Record" message:@"Are you sure to delete the record." delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
alert.tag = indexPath.row;
[alert show];
}
}
在Alert delegate
方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)//YES button clicked
{
[[self categoriesArray]removeObjectAtIndex:alertView.tag];
}
}
答案 2 :(得分:1)
UITableView
通过以下方式向您提供此信息:
- (NSIndexPath *)indexPathForSelectedRow
或者如果做出了多项选择:
- (NSArray *)indexPathsForSelectedRows
答案 3 :(得分:1)
在此委托方法中使用警报视图
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
然后在用户单击“删除”时执行您想要执行的操作。如果用户在alertview中预先删除,则必须使用alert view delegate方法来处理它。