2012-03-01 79 views
0

我有这样的代码从数据库的UITableView编辑惑

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

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [appDelegate.indexArray removeObjectAtIndex:indexPath.section]; 
     [appDelegate.notesArray removeObjectAtIndex:indexPath.section]; 
     [tableViewObj reloadData]; 
     NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
     [appDelegate.data writeToFile:DataPath atomically:YES]; 
     [tableViewObj reloadData]; 
    } 
} 

它完美的作品删除单元格的内容,但我需要把上面的代码在我按一下按钮,我把这个按钮点击

-(IBAction)_clickbtndeltNoteall:(id)sender 
{ 
      [appDelegate.indexArray removeObjectAtIndex:indexPath.section]; 
      [appDelegate.notesArray removeObjectAtIndex:indexPath.section]; 
      [tableViewObj reloadData]; 
      NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
      [appDelegate.data writeToFile:DataPath atomically:YES]; 
      [tableViewObj reloadData]; 
} 

但我得到这个错误:“未声明的标识符索引路径”。如何解决这个问题?

+0

挑战你会得到这样的感觉。在.h文件中采用一个NSIndexPath对象,并在commitEditingStyle方法中赋值并在你的按钮动作方法中使用该对象 – Narayana 2012-03-01 10:26:03

回答

1

我建议保存在单身按下行的indexPath。以下是如何使用singleton class来存储对象并跨类使用它们的指南。

在这个例子中,你也可以声明一个实例变量,并在你的UITableView的合适的代理方法中按下该行时进行设置。可以这样做,像这样:

@interface 

NSIndexPath *tableViewPath; 

@implementation 

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

tableViewPath = indexPath; 

} 

您为您的按钮动作现在应该是这样的:

- (IBAction)_clickbtndeltNoteall:(id)sender 
{ 
     [appDelegate.indexArray removeObjectAtIndex:indexPath.section]; 
     [appDelegate.notesArray removeObjectAtIndex:indexPath.section]; 
     [tableViewObj reloadData]; 
     NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
     [appDelegate.data writeToFile:DataPath atomically:YES]; 
     [tableViewObj reloadData]; 
} 

该按钮的代码假定行已选定在它被按下之前。你也可以初始化indexPathviewDidLoad中 像这样:

@implementation 

- (void)viewDidLoad { 

tableViewPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

} 

你应该调整你的初始化什么是适合您的具体方案!

1

单击按钮时,您想要删除哪个单元格?

地址:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowToDelete inSection:sectionToDelete]; 

在你的方法顶部。

+0

这显然不是一个好的解决方案,因为它不确定哪一行需要删除?另外一个NSIndexPath的本地声明是一个坏主意。应该为此使用一个实例变量。 – wizH 2012-03-01 11:22:39

+0

你的意思是他应该在他的控制器上保留一个NSIndexPath实例,以知道要删除哪个索引?我同意他不需要NSIndexPath,但我可以向你保证一个实例变量不应该用于这个。 – fbernardo 2012-03-01 11:26:12

+0

如果他想保留整个班级的排名索引,我猜这会是理想的吗? – wizH 2012-03-01 11:28:10

1

indexPath没有在_clickbtndeltNoteall中定义,这就是为什么你会得到这个错误。

你可以做这样的事情

-(IBAction)_clickbtndeltNoteall:(id)sender 
{ 
     [appDelegate.indexArray removeObjectAtIndex:index]; 
     [appDelegate.notesArray removeObjectAtIndex:index]; 
     [tableViewObj reloadData]; 
     NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
     [appDelegate.data writeToFile:DataPath atomically:YES]; 
     [tableViewObj reloadData]; 
} 

其中index定义别的地方(例如类字段)。您可以使用一个辅助的方法来设置index指定要删除的部分,然后调用_clickbtndeltNoteall

0

您正在访问的indexPath可变的内部 - (IBAction为)_clickbtndeltNoteall:(ID)发送者,这是不可能的,因为该变量不存在于该范围内。

为什么你不把最后一个indexPath存储在一个全局变量中,然后你可以访问它?事情是这样的:

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

     if (editingStyle == UITableViewCellEditingStyleDelete) {  
     lastIndexPath = indexPath; 
     //Your code 
     } 
} 

而在你interfaz声明,你就把:

NSIndexPath *lastIndexPath; 
0
-(IBAction)_clickbtndeltNoteall:(id)sender 
{  
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview]; 
    NSIndexPath *clickedButtonPath = [tableViewObj indexPathForCell:clickedCell]; 
    int section1=clickedButtonPath.section; 
[appDelegate.indexArray removeObjectAtIndex:section1]; 
      [appDelegate.notesArray removeObjectAtIndex:section1]; 
      [tableViewObj reloadData]; 
      NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
      [appDelegate.data writeToFile:DataPath atomically:YES]; 
      [tableViewObj reloadData]; 
} 

如果按钮是对细胞itself..If按钮时,它一定会努力不上cell然后你可以直接通过indexpath没有指定uitableviewcell ...试试吧..