2014-12-06 113 views
0

我正在制作一个简单的待办事项列表应用程序。我已经完成了Apple开发中的tutroial(https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/FirstTutorial.html#//apple_ref/doc/uid/TP40011343-CH3-SW1)。现在,我正在尝试将此应用程序开发为全功能应用程序。我的第一个目标是能够删除表格视图单元格。我使用这个代码来获取删除按钮:(我使用的Xcode 6)删除表格视图单元格按钮崩溃

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [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 
    } 
} 

这使用户从右向左拖动一个单元格,删除按钮将被显示。但是,在点击按钮时,应用程序崩溃。异常断点显示此行导致错误。

我在网上搜索解决办法,据我所知,这种方法可能值得看看的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [self.toDoItems count]; 
} 

此外,一些其他的答案说了一些关于开始时间和endUpdates的方法中。如果有人遇到问题,我会很开心!

下面是表视图整个代码:

#import "XYZToDoListTableViewController.h" 
#import "XYZToDoItem.h" 
#import "XYZAddToDoItemViewController.h" 

@interface XYZToDoListTableViewController() 

@property NSMutableArray *toDoItems; 

@end 

@implementation XYZToDoListTableViewController 

- (void) loadInitialData { 
    XYZToDoItem *item1 = [[XYZToDoItem alloc] init]; 
    item1.itemName = @"Add to-do item"; 
    [self.toDoItems addObject:item1]; 
    XYZToDoItem *item2 = [[XYZToDoItem alloc] init]; 
    item2.itemName = @"Do your job"; 
    [self.toDoItems addObject:item2]; 
    XYZToDoItem *item3 = [[XYZToDoItem alloc] init]; 
    item3.itemName = @"Mark item as completed"; 
    [self.toDoItems addObject:item3]; 
} 

- (IBAction)unwindToList:(UIStoryboardSegue *)segue 
{ 
    XYZAddToDoItemViewController *source = [segue sourceViewController]; 
    XYZToDoItem *item = source.toDoItem; 
    if (item != nil) { 
     [self.toDoItems addObject:item]; 
     [self.tableView reloadData]; 
    } 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.toDoItems = [[NSMutableArray alloc] init]; 
    [self loadInitialData]; 
} 

    // 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; 


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

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 


    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [self.toDoItems count]; 
} 

/* 
// 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; 
} 
*/ 


- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [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 { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView 
         dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath]; 
    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row]; 
    cell.textLabel.text = toDoItem.itemName; 
    if (toDoItem.completed) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    } 
    return cell; 
} 


# pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    XYZToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row]; 
    tappedItem.completed = !tappedItem.completed; 
    [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone]; 
} 

@end 

非常感谢! (如果你需要更多的代码,请告诉我吧!)

回答

3

当你删除单元格,你需要从阵列中删除此行,你的情况从self.toDoItems

[tableView beginUpdates]; 
[self.toDoItems removeObjectAtIndex: indexPath.row]; 
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdates]; 
+0

现在,它的作品!非常感谢! – Developingdeveloper 2014-12-06 19:27:32

相关问题