Objective c 加载视图后无法更新UITableView

Objective c 加载视图后无法更新UITableView,objective-c,uitableview,refresh,Objective C,Uitableview,Refresh,我仍然是一个obj-C的noobish,我正在创建一个带有tableview的应用程序,当我向tableview添加项目时,需要刷新该应用程序。我在谷歌上搜索了一下,发现一些答案说我需要使用[self.tableView reloadData],但它对我不起作用 这是我的tableView代码 - (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView { return 1; } - (NSString *)

我仍然是一个obj-C的noobish,我正在创建一个带有tableview的应用程序,当我向tableview添加项目时,需要刷新该应用程序。我在谷歌上搜索了一下,发现一些答案说我需要使用
[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];

         }
     } }
我已经尝试过的是每次我向数据库中添加一些东西时调用populateTable函数,但是它仍然没有刷新,有人能帮我吗?我已经被那个问题困扰了一段时间了

谢谢

试试看

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.myTableView reloadData];
}

您需要在所有数据都位于您感测表的数组中后重新加载表。 在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;

 }

一旦更改了表的数据源,则只需重新加载表。只需检查是否在数据源受到影响后重新加载表视图。像这样

- (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]
 }

你的代码从来没有创建过一个单元格,使用调试器检查它返回的内容,我认为它将为零。Michael,在开始为你的应用程序编写代码之前,你应该先掌握iOS开发。它会在“self.myTableView”上出错。在我输入“self”之后,我的tableView甚至没有任何自动完成功能。你知道为什么吗?我不知道这是怎么回事,因为我可以在tableView中使用这些值,我只是不能更新新的值。ThanksibuitableView*myTableView;现在将其声明为.h文件,现在将其设置为.xib文件[myTableView重载数据];workedmy UITableView保持为空:/,也许我应该提到我没有使用Xib文件,但我使用的是故事板?@Michael:然后在故事板中分配tableView我的代理和数据源都链接到我的第二个ViewController(tableView所在的视图),tableView链接到插座。UiTableView仍不更新/重新加载其数据:/entries数组是否确实从数据库获取所有数据??或者使用其他方法获取数据,并确保使用NSLog使用数据更新阵列。获取所有数据后,添加[urTableView reloadData];这是标准的实现方式。我可以在tableView中看到值,所以我想我可以从数据库中访问数据。。我的问题是,在我向数据库添加新内容后,在数据库中添加了一些值后,我无法刷新我的ViewTable,您是否再次调用populateTable方法来更新数组??还要检查数据库是否得到更新??在你的生活中。。方法,则每次都要分配数组。取而代之的是,“entries=[[NSMutableArray alloc]init];”将其添加到viewDidLoad()中,问题是它不会刷新。我的应用程序有三个选项卡,第二个是带有tableView的选项卡。它只保存我第一次单击选项卡时的状态。因此,如果我添加了一些内容并进入第二个选项卡,该值将被添加,但如果我再次添加,它将保持在“旧”版本。