在进入问题之前,我必须声明我已经检查了有关此问题的其他问题。
我在从 UITableView 中删除行时遇到问题。
我的应用程序允许用户选择一些联系人并在 UITableview 中显示他们的姓名和电话号码,并将这些信息保存在两个可变数组中,分别称为 people 和 numbers。
这是我用来删除单元格的方法。
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//[numbers removeObjectAtIndex:indexPath.row];
//[people removeObjectAtIndex:indexPath.row];
[self.tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
}
//[tableView endUpdates];
[tableView reloadData];}
这是我的数据设置
#import <UIKit/UIKit.h>
@import AddressBook;
@import AddressBookUI;
@interface addedContacts : UIViewController <UITextFieldDelegate, ABPeoplePickerNavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource>{
NSMutableArray* people;
NSMutableArray* numbers;
IBOutlet UITableView *tableview;
}
@property (strong, nonatomic) IBOutlet UITableView *mTV;
- (IBAction)addPerson:(id)sender;
- (IBAction)editButton:(id)sender;
@property (nonatomic, strong) NSMutableArray *tableData;
@end
我在 .m 文件的开头定义了 tableview IBOutlet 的属性。
-(void)viewDidLoad{
[super viewDidLoad];
tableview.delegate = self;
tableview.dataSource = self;}
这是我得到的异常;
2015-07-14 16:16:06.207 MyTestApp[716:607] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:1368
2015-07-14 16:16:06.491 MyTestApp[716:607] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6),
plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
那么,我哪里做错了?
如果需要,我可以提供第一个抛出的调用堆栈或更多代码。
更多信息
-(NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger)section
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
numbers = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"numbers"]];
return [numbers count];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
最佳答案
您应该为您的 numberOfRowsInSection
方法返回 self.tableData.count
。截至目前,您并未从 numberOfRowsInSection
返回的数据源中删除,因此您的计数已关闭。
您也不应该在那里访问 NSUserDefaults
。在您的 viewDidLoad
中,您应该从 NSUserDefaults
加载所有内容,允许用户对数据进行更改和更新。然后,当您离开 View 时,将所有内容保存回 NSUserDefaults
。
关于iOS - 从 UITableView 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407979/