我仍然是一个obj-C,但我不清楚,我正在创建一个应用程序,当我添加项目到tableview时需要刷新一个tableview。我GOOGLE了一下,发现一些答案,说我需要使用[self.tableView reloadData]
,但它不适合我。加载视图后无法更新UITableView
这里是我的代码为我的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];
}
} }
我已经尝试做的是调用populateTable功能每次我添加一些代码到数据库,但仍然不刷新,请有人可以帮我吗?一段时间以来一直困扰着这个问题。
感谢
您的代码不会创建一个单元格开始,使用调试器检查它返回的内容,我认为它将为零。 – Tim
迈克尔,在开始为你编写代码之前,你应该先掌握iOS开发。 – samfisher