开发者

refresh TableView after clearing data source object

The issue is when i'm trying to remove all entries in my data source object and so clear all the rows in my tableview, i'm succeeding with the first part, the counter of rows to show (tableView:numberOfRowsInSection: method) returns 0, but the cells still contain obsolete data.

(Have little reputation here to post images, so i downloaded it to ftp: history.png)

Here's an example of what i'm trying to do. The trash bin icon clears the database, sets rows count to zero, but these cells remain in the table, which causes crashes when scrolling it.

Data source object stores as a property in AppDelegate, so ViewController gets data from there.

Here's some 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]; // removes entries from DB
    [appDelegate initHistoryList]; // refreshes data source object
    [self.tableView reloadData]; // doesn't help
}

AppDelegate has (NSMutableArray) *historyList property and this function:

- (void)initHistoryList {
    HistoryDataController *historyDataController = [[HistoryDataController alloc] initWithHistoryData];
    historyList = [historyDataController.historyEntries retain];
    [historyDataController release];
}

I believe the problem is that TableView somehow caches the values of it's cells, but why do they display when tableView:numberOfRowsInSection: returns 0? And how to purge that cache?

Thanks for help.


I think your problem is the line [self.tableView reloadData]; What you are clearing is the tableView of the UITableViewController and not the historyTable which I'm guessing you are using.

So a [self.historyTable reloadData]; should do the trick.


Without more information about your setup, I cannot be sure, but this is what I think is happening.

There are two tables in your view. UITableViewController automatically creates its own table if it is not given one by the Nib. Fix this by using the tableView property in Interface Builder and remove the historyTable property.

The dataSource property on your table is not properly set. Make sure it is set to your HistoryViewController object, or it will not know where to get the data from.

Also, you have a memory leak in your initHistoryList method. The old historyList value is not released, so it will be leaked. But why do you have to call both [HistoryDataController clearHistory] and [appDelegate initHistoryList]? Shouldn't clearing the database clear the array too? If not, you should be able to just create a new NSMutableArray in initHistoryList instead of creating and releasing a HistoryDataController.


Check your UITableView IBOutlet. If it's improperly connected, your call to reloadData would do nothing.


So the problem definetely was in misunderstanding of TableView delegate and outlets. For example, - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section should be replaced with - (NSInteger)tableView:historyTable [...], thus awoiding creation of TableView instance and it allows us to call reloadData for a proper TableView instance in our - (IBAction)clearHistory:(id)sender method:

- (IBAction)clearHistory:(id)sender {
    [HistoryDataController clearHistory];
    [appDelegate.historyList removeAllObjects];
    [appDelegate initHistoryList];
    [self.historyTable reloadData];
}

Thanks everyone for your help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜