gpt4 book ai didi

iphone - 清除数据源对象后刷新 TableView

转载 作者:搜寻专家 更新时间:2023-10-30 20:23:10 25 4
gpt4 key购买 nike

问题是当我试图删除我的数据源对象中的所有条目并清除我的 TableView 中的所有行时,我成功地完成了第一部分,即要显示的行的计数器(tableView :numberOfRowsInSection: 方法)返回 0,但单元格仍包含过时数据。

(这里没有什么名气可以发图,所以我下载到ftp:history.png)

这是我尝试做的一个例子。垃圾桶图标会清除数据库,将行计数设置为零,但这些单元格仍保留在表中,这会导致滚动时崩溃。

数据源对象作为属性存储在 AppDelegate 中,因此 ViewController 从那里获取数据。

这是一些代码:

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(NSMutableArray) *historyList 属性和这个函数:

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

我认为问题在于 TableView 以某种方式缓存了它的单元格的值,但为什么当 tableView:numberOfRowsInSection: 返回 0 时它们会显示?以及如何清除该缓存?

感谢您的帮助。

最佳答案

我认为您的问题是行 [self.tableView reloadData]; 您正在清除的是 UITableViewController 的 tableView 而不是我猜您正在使用的 historyTable。

所以 [self.historyTable reloadData]; 应该可以解决问题。

关于iphone - 清除数据源对象后刷新 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4239512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com