我的应用程序中有一个UITableView,我有用户将行添加到表中。我这样做,通过调用“insertRowAtIndexPath”:由于某种原因在iOS中调用insertRowToIndexPath之后不保存数据
- (IBAction)addRow:(id)sender {
NSString *theObjectToInsert = [NSString stringWithFormat:@"Row #: %lu", (unsigned long)[self.tableData count]];
[self.tableData addObject:theObjectToInsert];
NSLog(@"What is the count of the collection? %lu", self.tableData.count-1);
NSIndexPath *newPath=[NSIndexPath indexPathForRow:self.tableData.count-1 inSection:0];
[self.myTable insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.myTable scrollToRowAtIndexPath:newPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
我,我已经发现的问题是,添加必要的行后,我没有保存数据,因为我认为我应该。这里是应该存储数据的方法:
- (void)saveAction {
//I iterate through my entire UITableView to tally up the data from all of the rows
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [self.myTable numberOfSections]; ++j) {
for (NSInteger i = 0; i < [self.myTable numberOfRowsInSection:j]; ++i) {
if ([self.myTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]) {
[cells addObject:[self.myTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}
}
NSLog(@"The number of table rows are: %lu", (unsigned long)[cells count]);
}
出于某种原因,这只打印出“4”,即使我已经添加了更多。任何人都可以看到我做错了什么吗?
为什么你将UITableViewCell保存在单元格数组中?而你已经有self.tableData数组中的所有数据 –