2014-09-30 98 views
0

我想删除表中的行,当动作片删除按钮按下然后我写这样表行动表行的数据删除删除按钮按下

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UIActionSheet *action=[[UIActionSheet alloc]initWithTitle:@"Are You Sure" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Delete", nil]; 
     [action showInView:self.view]; 
    } 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
    NSString *buttontitle=[actionSheet buttonTitleAtIndex:buttonIndex]; 
    if ([buttontitle isEqualToString:@"Delete"]) 
    { 
     [self.map removeObjectAtIndex:indexpath.row]; 
     //tableView is global object of your table view. 
     [tableView reloadData]; 
    } 

    } 

一个代码,但错误是“Undeclare标识符indexpath”

回答

0

因为这里您的indexPath变量对您的方法而言是本地的,所以您不能将其访问到另一个方法中。

对于这个不是你应该做一个全局变量,并保存在该变量该指数的路径和您要那么任何访问它。

或者设置标签到你的动作片,然后访问它。

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UIActionSheet *action=[[UIActionSheet alloc]initWithTitle:@"Are You Sure" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Delete", nil]; 
     //set the index path as tag to action sheet. 
     action.tag = indexPath.row; 
     [action showInView:self.view]; 
    } 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
    NSString *buttontitle=[actionSheet buttonTitleAtIndex:buttonIndex]; 
    if ([buttontitle isEqualToString:@"Delete"]) 
    { 
     //access that tag here. 
     [self.map removeObjectAtIndex:action.tag]; 
     //here your tableView is global variable 
     [tableView reloadData]; 
    } 

    } 
+0

您还可以检查动作片标签内that.if(buttonIndex == 0){ // 这里访问的标签。 [self.map removeObjectAtIndex:action.tag] ;; [UITableView reloadData]; }而不是标题。 – iHulk 2014-09-30 07:44:13

+0

但然后选择“刷新数据”未发现的错误没有已知的类方法是因为reloadData是一个对象类型的方法不是一个类类型为此,你需要使表对象全球化,所以,你可以访问它发生@IDeveloper – 2014-09-30 08:40:00

+0

这里。我已经更新了我的答案再次检查。 – iHulk 2014-09-30 08:48:13

0

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 

您没有要删除的行的索引路径。你要么必须从表视图得到它或保存传入的索引路径通过

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

,并在动作片回调重用。

总之,你的做法似乎并没有采取API的优势。 UITableViewDelegate的方法允许用户看到“删除”按钮并按下或取消它。我建议你检查一下这些,以便你的应用可以工作,并且像其他iOS应用一样。

tableView:willBeginEditingRowAtIndexPath: 

tableView:editingStyleForRowAtIndexPath: 

tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: