2014-04-14 70 views
2
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.effectsTableView beginUpdates]; 

     Effect *effectToBeDeleted =self.effectsArray[indexPath.row]; 
     [self deleteEffectWithName:effectToBeDeleted.name]; 

     [self.effectsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop]; 
     [self.effectsArray removeObjectAtIndex:indexPath.row]; 

     [self.effectsTableView endUpdates]; 
    } 
} 

上述函數在理論上應該刪除行,因爲它們被滑動並且刪除按鈕被按下。然而該行,而不是不刪除甚至嘗試從UITableView刪除行後應用程序崩潰

[self.effectsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];

後可見被調用。當用戶滾動到UITableView的底部,加載最後一行後,該應用程序顯然崩潰,因爲其中一個對象已經從數組中刪除,因此出現"*** -[__NSArrayM objectAtIndex:]: index 9 beyond bounds [0 .. 8]"錯誤。

中的cellForRowAtIndexPath如下:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (!self.effectsArray) 
    { 
     [self loadEffectsInArray]; 
    } 

    static NSString *cellIdentifier = @"EffectsCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    } 


    Effect *effectCellEffect = [self.effectsArray objectAtIndex:indexPath.row]; 
    NSString *effectCellText = effectCellEffect.name; 
    [cell.textLabel setText:effectCellText]; 
    cell.textLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; 

    return cell; 
} 

下面是更好的背景下,整個.m文件:

#import "EffectsManagementTableViewController.h" 
#import "Effect+Manage.h" 
#import "VLOGAppDelegate.h" 
#import "AddEffectViewController.h" 
#import "EffectFilterProperty+Manage.h" 
#import "Effect.h" 

@interface EffectsManagementTableViewController() 

@property (strong, nonatomic) IBOutlet UITableView *effectsTableView; 
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic, strong) NSMutableArray *effectsArray; 
@property (nonatomic, strong) NSString *currSelectedRowTitle; 

@end 

@implementation EffectsManagementTableViewController 

-(NSString *)currSelectedRowTitle 
{ 
    if(!_currSelectedRowTitle) 
    { 
     _currSelectedRowTitle = @""; 
    } 
    return _currSelectedRowTitle; 
} 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 

    VLOGAppDelegate* appDelegate = [UIApplication sharedApplication].delegate; 
    self.managedObjectContext = appDelegate.managedObjectContext; 
    self.effectsTableView.dataSource = self; 
    self.effectsTableView.delegate = self; 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    [self loadEffectsInArray]; 
    [self.effectsTableView reloadData]; 
    [self.effectsTableView setNeedsDisplay]; 
    [self.effectsTableView setNeedsLayout]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (!self.effectsArray) 
    { 
     [self loadEffectsInArray]; 
    } 
    return [self.effectsArray count]; 
} 

//4 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (!self.effectsArray) 
    { 
     [self loadEffectsInArray]; 
    } 

    static NSString *cellIdentifier = @"EffectsCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    } 

    //6 
    Effect *effectCellEffect = [self.effectsArray objectAtIndex:indexPath.row]; 
    NSString *effectCellText = effectCellEffect.name; 
    //7 
    [cell.textLabel setText:effectCellText]; 
    //[cell.detailTextLabel setText:@"5 stars!"]; 
    cell.textLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; 
    //cell.backgroundColor = [UIColor blackColor]; 
    //cell.textLabel.textColor = [UIColor whiteColor]; 
    //cell.detailTextLabel.textColor = [UIColor grayColor]; 
    //cell.textLabel.highlightedTextColor = self.effectsTableView.tintColor; 

    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.effectsTableView beginUpdates]; 

     Effect *effectToBeDeleted =self.effectsArray[indexPath.row]; 
     [self deleteEffectWithName:effectToBeDeleted.name]; 

     [self.effectsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop]; 
     [self.effectsArray removeObjectAtIndex:indexPath.row]; 


     //[self loadEffectsInArray]; 
     [self.effectsTableView endUpdates]; 
     //[self.effectsTableView reloadData]; 
     //[self.effectsTableView setNeedsLayout]; 
     //[self.effectsTableView setNeedsDisplay]; 
    } 
} 

-(void)deleteEffectWithName:(NSString *)effectName 
{ 
    NSArray *efpForEffectToBeDeleted = [EffectFilterProperty getEffectFilterPropertiesForEffectName:effectName forImmediateEngagement:NO forManagedObjectContext:self.managedObjectContext]; 
    Effect *effectToBeDeleted = [Effect getEffectWithName:effectName forManagedObjectContext:self.managedObjectContext]; 

    for (int i = 0; i < efpForEffectToBeDeleted.count; i++) 
    { 
     EffectFilterProperty *currEFP = efpForEffectToBeDeleted[i]; 
     currEFP.relatedEffect = nil; 
     currEFP.relatedFilterProperty = nil; 
     [self.managedObjectContext deleteObject:currEFP]; 
    } 

    [self.managedObjectContext deleteObject:effectToBeDeleted]; 

    NSError *err = nil; 
    [self.managedObjectContext save:&err]; 
    if (err != nil) { 
     //Problem while saving 
    } 
} 

-(void)loadEffectsInArray 
{ 
    self.effectsArray = [[Effect getAllEffectsForManagedObjectContext:_managedObjectContext] mutableCopy]; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Effect *effectCellEffect = [self.effectsArray objectAtIndex:indexPath.row]; 
    self.currSelectedRowTitle = effectCellEffect.name; 
    [self performSegueWithIdentifier:@"PushedByTableView" sender:self]; 
} 

/* 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath]; 

    // Configure the cell... 

    return cell; 
} 
*/ 

/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 


#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    // Make sure your segue name in storyboard is the same as this line 
    if ([[segue identifier] isEqualToString:@"PushedByTableView"]) 
    { 
     AddEffectViewController *destinationViewController = segue.destinationViewController; 
     NSString *selectedRowText = self.currSelectedRowTitle; 
     destinationViewController.effectToManage = [Effect getEffectWithName:selectedRowText forManagedObjectContext:self.managedObjectContext]; 
    } 
} 


@end 

什麼我錯在這裏做什麼?

+0

請檢查self.effectsArray'的'計數。 – iDeveloper

+0

數組沒有錯......它有我想要的行數。唯一的問題是即使執行上面的代碼,行也不會消失。 –

+0

您是否嘗試過@Charan sir解決方案? – iDeveloper

回答

3

你在哪裏開始你的更新,請刪除您行,無需重新加載當你正在寫的更新開始和結束的更新表的前添加

[self.effectsTableView beginUpdates]; 

請不要在刪除行之前從數組中刪除對象。

+0

起初,我已經完成了......但我認爲這開始更新可能工作。 –

+0

我把開始更新放在方法的頂部。不工作:( –

+0

和你刪除reloadData也 – Charan

0

試試這個代碼添加beginUpdates方法之前刪除名爲

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [self.effectsTableView beginUpdates]; 
     Effect *effectToBeDeleted =self.effectsArray[indexPath.row]; 
     [self deleteEffectWithName:effectToBeDeleted.name]; 

     [self.effectsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop]; 

     [self.effectsArray removeObjectAtIndex:indexPath.row]; 
     [self loadEffectsInArray]; 
     [self.effectsTableView endUpdates]; 

    } 
+0

做到了。不起作用。 –

0

試試這個代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
if (editingStyle == UITableViewCellEditingStyleDelete) { 

    Effect *effectToBeDeleted =[self.effectsArray objectAtIndex:indexPath.row]; 
    [self deleteEffectWithName:effectToBeDeleted.name]; 
    [self.effectsArray removeObjectAtIndex:indexPath.row]; 
    [self loadEffectsInArray]; 
    [self.effectsTableView reloadData]; 

} 
+0

沒有變化。該行並未從UITableView中刪除。數組對象正在被刪除。因此不匹配。 –

0

由於這個消息,這是暴跌的原因出界,所以嘗試檢查數組索引分配之前:

Effect *effectToBeDeleted = self.effectsArray > indexPath.row ? self.effectsArray[indexPath.row] : nil; 

and

if(self.effectsArray.count > indexpath.row) 
[self.effectsArray removeObjectAtIndex:indexPath.row]; 
else 
NSlog("out of bound"); 
+0

實際上出乎意料的是,正如你可能理解的那樣,UITableView從effectsArray中加載,無論何時一行離開屏幕並再次返回,UITableView都會調用cellForRowAtIndexPath方法來獲取標題和所有內容。現在這裏發生的是數組中的刪除行從數組中減少了一個對象,但由於某種原因,UITableView中的行不會消失。那就是問題所在。 –

2

它的工作 在你commitEditingStyle方法的末尾添加這一行,這將幫助你

[TableViewForFirstView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
相關問題