我仍然使用obj-C但是我正在创建一个带有tableview的应用程序,当我向tableview添加项目时需要刷新它。我用Google搜索并找到了一些答案,说我需要使用[self.tableView reloadData]
,但它对我不起作用。
这是我的tableView
的代码- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView {
return 1; }
- (NSString *) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section {
NSString *myTitle = [[NSString alloc] initWithFormat:@"Runs"];
return myTitle;
}
- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
return [entries count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
// Set up the cell...
NSString *cellValue = [entries objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
这是我填充表格的代码
- (void)populateTable{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *username = appDelegate.username;
entries = [[NSMutableArray alloc]init ];
[self openDB];
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Savings WHERE username = '%@'",username];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(runnerAppDB, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
{
while(sqlite3_step(statement)==SQLITE_ROW)
{
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc] initWithUTF8String:field1];
// username - distance - speed - date - comment
NSString *str = [[NSString alloc] initWithFormat:@"%@ %@ %@ - %@", field1Str];
[entries addObject:str];
}
} }
我已经尝试过的是每次向db添加内容时都调用populateTable函数,但是它仍然没有刷新,有人可以帮我吗?一段时间以来一直坚持这个问题。
由于
答案 0 :(得分:1)
试试吧......
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.myTableView reloadData];
}
答案 1 :(得分:0)
您需要在阵列中的所有数据之后重新加载表格。 在tableview的cellforrowatindexpath方法中,您需要重用该单元格。请更新您的cellforrow方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"ArticleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
NSString *cellValue = [entries objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
答案 2 :(得分:0)
一旦表的数据源发生变化,那么只需要重新加载表。只需检查数据源受影响后是否重新加载表视图。像这样。
- (void)populateTable{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *username = appDelegate.username;
entries = [[NSMutableArray alloc]init ];
[self openDB];
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Savings WHERE username = '%@'",username];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(runnerAppDB, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
{
while(sqlite3_step(statement)==SQLITE_ROW)
{
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc] initWithUTF8String:field1];
// username - distance - speed - date - comment
NSString *str = [[NSString alloc] initWithFormat:@"%@ %@ %@ - %@", field1Str];
[entries addObject:str];
}
}
[self.tableView reloadData]
}