The problem is that when I try to delete all records in my data source object and therefore clear all the rows in my table view, I succeed in the first part, the row counter for display (method tableView:numberOfRowsInSection:) returns 0, but the cells still contain stale data.
(I have little reputation to send images, so I uploaded them to ftp: history.png )
Here is an example of what I'm trying to do. The recycle bin icon deletes the database, sets the number of rows to zero, but these cells remain in the table, which leads to crashes when scrolling.
The data source object is stored as a property in AppDelegate, so the ViewController gets the data from there.
Here is the code:
HistoryViewController.h
@interface HistoryViewController : UITableViewController <UITableViewDataSource> {
IBOutlet UITableView *historyTable;
SynonymsAppDelegate *appDelegate;
}
@property (retain) UITableView *historyTable;
- (IBAction)clearHistory:(id) sender;
@end
HistoryViewController.m
@implementation HistoryViewController
@synthesize historyTable;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
appDelegate = (SynonymsAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate initHistoryList];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.historyList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
HistoryModel *historyEntry = [appDelegate.historyList objectAtIndex:indexPath.row];
cell.textLabel.text = historyEntry.word;
return cell;
}
- (IBAction)clearHistory:(id)sender {
[HistoryDataController clearHistory];
[appDelegate initHistoryList];
[self.tableView reloadData];
}
AppDelegate (NSMutableArray) *historyList :
- (void)initHistoryList {
HistoryDataController *historyDataController = [[HistoryDataController alloc] initWithHistoryData];
historyList = [historyDataController.historyEntries retain];
[historyDataController release];
}
, , TableView - , , t ableView:numberOfRowsInSection: 0? ?
.